1232133d2ffa
1232133d2ffa

Reputation: 191

How to set Connection String with Entity Framework

I am using EF6 with MySQL and have a Model that I will use for MULTIPLE Databases.

I would like to be able to set the connections settings in my Form.

How do I set the Connection String for my Model programatically?

Upvotes: 0

Views: 4286

Answers (2)

Krishanu Dey
Krishanu Dey

Reputation: 6406

you should use EntityConnectionFactory
Here is what you need.

public string CreateConnectionString(string BasicConnectionString)
{
    //EntityConnectionFactory 
    var entityConnectionStringBuilder= new EntityConnectionStringBuilder();
    entityConnectionStringBuilder.Provider = "Your Provicer here"      //For me it is "System.Data.SqlClient";
    entityConnectionStringBuilder.ProviderConnectionString = BasicConnectionString;
    entityConnectionStringBuilder.Metadata = "res://*";
    return entityConnectionStringBuilder.ToString();
}

Here is an sample usage

MyContext ctx = new MyContext(CreateConnectionString())

:: Update ::

As you are using DB first method, see the following image

pic

when these two radio buttons are available, select the first one. Then you will be able to set the connection string of your model.

Here is how my context looks like (Though it is object context. But doesn't matter in context of this question)

public partial class DataContext : ObjectContext
    {
        #region Constructors

        /// <summary>
        /// Initializes a new DataContext object using the connection string found in the 'DataContext' section of the application configuration file.
        /// </summary>
        public DataContext() : base("name=DataContext", "DataContext")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new DataContext object.
        /// </summary>
        public DataContext(string connectionString) : base(connectionString, "DataContext")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new DataContext object.
        /// </summary>
        public DataContext(EntityConnection connection) : base(connection, "DataContext")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }

        #endregion

        #region Partial Methods

        partial void OnContextCreated();

        #endregion
    ...
    }

Update

Add the constructor you are looking for in a partial class outside of the auto-generated entity class:

public partial class WMSChennaiDEVEntities : DbContext
{
    public WMSChennaiDEVEntities(string connectionstring)
            : base(connectionstring)
    {
    }
}

This constructor is not included in EF 5/6 apparently to prevent us from accidentally passing a sql connection string when an entity connection string is desired.

Upvotes: 1

ZZZ
ZZZ

Reputation: 2812

you have to hardcode the connection string somewhere. The common place is app.config.

    <connectionStrings>

<add name="Connection1" connectionString="Server=localhost\ServerInstance;Database=MyDB;Trusted_Connection=True;" providerName="System.Data.SqlClient" />

Then in your Code First model, do the following:

    public class MyContext : DbContext
{
    public MyContext():base("Connection1")
    {...}

You see that BCL db library and EF were all designed for such usage pattern.

Changing connection strings in UI is not desired in business applications since users won't change db location very often, unless you are developing a DB admin app or an installer.

Upvotes: 1

Related Questions