HOW TO: dynamic connection string for entity framework

前端 未结 2 1323
梦毁少年i
梦毁少年i 2021-01-17 01:15

Like the title. How can I do it?

I tried something, but it doesn\'t work like I was expecting.

I\'m using an Entity Framework model. I need to pass my connec

2条回答
  •  清歌不尽
    2021-01-17 01:19

    Your approach is correct, but you need to remember the connection string for EF requires the metadata and so on. So use the EntityConnectionStringBuilder. For example:

    // the model name in the app.config connection string (any model name - Model1?)
    private static string GetConnectionString(string model, YourSettings settings)
    {
        // Build the provider connection string with configurable settings
        var providerSB = new SqlConnectionStringBuilder
        {
            // You can also pass the sql connection string as a parameter instead of settings
            InitialCatalog = settings.InitialCatalog,
            DataSource = settings.DataSource,
            UserID = settings.User,
            Password = settings.Password
        };
    
        var efConnection = new EntityConnectionStringBuilder();
        // or the config file based connection without provider connection string
        // var efConnection = new EntityConnectionStringBuilder(@"metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=System.Data.SqlClient;");
        efConnection.Provider = "System.Data.SqlClient";
        efConnection.ProviderConnectionString = providerSB.ConnectionString;
        // based on whether you choose to supply the app.config connection string to the constructor
        efConnection.Metadata = string.Format("res://*/Model.{0}.csdl|res://*/Model.{0}.ssdl|res://*/Model.{0}.msl", model); ;
        return efConnection.ToString();
    
    }
    // Or just pass the connection string
    private static string GetConnectionString(string model, string providerConnectionString)
    {
    
        var efConnection = new EntityConnectionStringBuilder();
        // or the config file based connection without provider connection string
        // var efConnection = new EntityConnectionStringBuilder(@"metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=System.Data.SqlClient;");
        efConnection.Provider = "System.Data.SqlClient";
        efConnection.ProviderConnectionString = providerConnectionString;
        // based on whether you choose to supply the app.config connection string to the constructor
        efConnection.Metadata = string.Format("res://*/Model.{0}.csdl|res://*/Model.{0}.ssdl|res://*/Model.{0}.msl", model);
        // Make sure the "res://*/..." matches what's already in your config file.
        return efConnection.ToString();
    
    }
    

    EDIT

    The exception you get is because when you pass a pure SQL connection string, it assumes you are working with Code first, so it calls the OnModelCreation event. When you include the MetaData section as shown above, that tells EF it's a complete EF connection string.

提交回复
热议问题