0

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?

1232133d2ffa
  • 191
  • 1
  • 13

2 Answers2

1

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.

Krishanu Dey
  • 6,326
  • 7
  • 51
  • 69
  • @Smarter :: Which EF Version and VS version? can you post a smaple connection string for your DB? – Krishanu Dey May 28 '14 at 04:29
  • EF6, VS2013Ultimate. The connection string isn't the issue, implementing it is what I am asking? My Entity is auto-generated by VS (ADO.NET Entity Data Model), how do I implement the Connection String into it? – 1232133d2ffa May 28 '14 at 05:02
  • I assured that the first setting was selected and I recieve the error 'Dialer_File_Builder.hyperion_collectionsmaxEntities' does not contain a constructor that takes 1 arguments - http://puu.sh/94q53/d8e15f54ce.png Declaration: http://puu.sh/94q8M/78e4086e65.png – 1232133d2ffa May 28 '14 at 05:25
  • I see how I can implement the connection string, but it seems it will break the auto-generated implementation: http://puu.sh/94qgt/7f586a6ff6.png – 1232133d2ffa May 28 '14 at 05:28
  • I figured it out, it was as simple as changing base("hyp*") to base(s) =D. – 1232133d2ffa May 28 '14 at 05:42
  • Updated answer, Please check. – Krishanu Dey May 28 '14 at 05:46
1

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.

ZZZ
  • 2,752
  • 2
  • 25
  • 37