Entity Framework 6 set connection string runtime

后端 未结 3 710
日久生厌
日久生厌 2020-12-08 07:50

We are in a mixed environment where our application is using both ADO.NET and Entity Framework.
Since both are pointing to the same physical SQL server, we would like to

相关标签:
3条回答
  • 2020-12-08 08:14

    You can work at design time using the connection string in your config file.

    <add name="DWContext" connectionString="metadata=res://*/Database.DWH.DWModel.csdl|res://*/Database.DWH.DWModel.ssdl|res://*/Database.DWH.DWModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SQLSERVER_INSTANCE;initial catalog=DB_NAME;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    

    So don't remove it because you need it ONLY at design time.

    Work instead in a dynamic way at runtime using this approach (similar to your last one):

    Extend the data context partial class:

    public partial class DWContext
    {
        public DWContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
        }
    
        /// <summary>
        /// Create a new EF6 dynamic data context using the specified provider connection string.
        /// </summary>
        /// <param name="providerConnectionString">Provider connection string to use. Usually a standart ADO.NET connection string.</param>
        /// <returns></returns>
        public static DWContext Create(string providerConnectionString)
        {
            var entityBuilder = new EntityConnectionStringBuilder();
    
            // use your ADO.NET connection string
            entityBuilder.ProviderConnectionString = providerConnectionString;
    
            entityBuilder.Provider = "System.Data.SqlClient";
    
            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/Database.DWH.DWModel.csdl|res://*/Database.DWH.DWModel.ssdl|res://*/Database.DWH.DWModel.msl";
    
            return new DWContext(entityBuilder.ConnectionString);
        }
    
    }
    

    And from your code create a new EF data context with:

    private DWContext db = DWContext.Create(providerConnectionString);
    

    Ciao ;-)

    0 讨论(0)
  • 2020-12-08 08:14

    Other way is to declare another connection string in the config file and use it with the following alternative constructor:

    class TestEntities : DbConnect {
     
        public TestEntities (string connectionName)
           : base($"name={connectionName}")
        {
        }
    
    ...
    

    Then to use this solution just:

    1. add another connection to the config file:
     <add name="other" connectionString="metadata=res://*/Data.TestEntities .csdl|res://*/Data.TestEntities .ssdl|res://*/Data.TestEntities .msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=...;initial catalog=...;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    
    1. call it like this:

      var db = new TestEntities (connectionName:"other");
      
    0 讨论(0)
  • 2020-12-08 08:17

    You are getting the Code First mode exception because you are passing a DbConnection built with the ADO.NET connection string. This connection string does not include references to metadata files, so EntityFramework does not know where to find them.

    To create a DbContext with an appropriate programmatically set connection string, use the EntityConnectionStringBuilder class.

    var entityBuilder = new EntityConnectionStringBuilder();
    
    // use your ADO.NET connection string
    entityBuilder.ProviderConnectionString = conString;
    
    // Set the Metadata location.
    entityBuilder.Metadata = @"res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl";
    var dbContext = new DbContext(entityBuilder.ConnectionString);
    
    0 讨论(0)
提交回复
热议问题