Configure multiple database Entity Framework 6

后端 未结 3 582
予麋鹿
予麋鹿 2020-11-30 03:18

In my solution I have 2 projects that use Entity Framework 6. Each points to a different database, both using the same data provide - SQL Server. A third project in my solut

相关标签:
3条回答
  • 2020-11-30 03:46

    Here is what I did for two DB with EF6

    Web.config

          <connectionStrings>
            <add name="EntityContainer" connectionString="metadata=res://WebService/Database.Database.csdl|res://WebService/Database.Database.ssdl|res://WebService/Database.Database.msl; .../>
            <add name="ArchiveEntityContainer" connectionString="metadata=res://WebService/Database.Database.csdl|res://WebService/Database.Database.ssdl|res://WebService/Database.Database.msl; .../>
          </connectionStrings>
    

    Add second constructor in Database.Context.tt (be careful: auto-generated code)

    public <#=code.Escape(container)#>(string connectionString)
        : base(connectionString)
    {
    }
    

    Use

    using (EntityContainer context = new EntityContainer())
    {
        //...
    }
    
    using (EntityContainer context = new EntityContainer("ArchiveEntityContainer"))
    {
        //...
    }
    
    0 讨论(0)
  • 2020-11-30 03:48

    It's not important that how many DbContexts you have(In entity framework 6). Just put connection strings in appConfig or webConfig of startup project.

    Then you're ready to go.

    Example of appConfig with two connectionString with Ef 6.01 & Sql Compact 4.0

    <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="MainDb" connectionString="Data Source=|DataDirectory|\Db.sdf" providerName="System.Data.SqlServerCe.4.0" />
        <add name="AnotherDb" connectionString="Data Source=|DataDirectory|\AnotherDb.sdf" providerName="System.Data.SqlServerCe.4.0" />
      </connectionStrings>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="System.Data.SqlServerCe.4.0" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
        </providers>
      </entityFramework>
    

    And example of DbContexts:

    public class AppDb : DbContext
    {
        public AppDb()
            : base("MainDb")
        {
    
        }
    }
    
    public class AnotherDb : DbContext
    {
        public AnotherDb()
            : base("AnotherDb")
        {
    
        }
    }
    

    It's not important that your contexts are in separated projects or not, only Config of startup project is important.

    Let me know if any other information you need.

    Good luck

    0 讨论(0)
  • 2020-11-30 03:59

    Connection string of EntityFramework 6 should be inside configuration file that located (alert!) in execution folder. For example OP have multiple projects in solution, so the connection string must be in configuration file belongs to main executive project.

    Now, if you want to define connection string in your code, you can make fake connection string in configuration file and give your entity's instance new connection string:

    DBEntities e = new DBEntities();
    e.Database.Connection.ConnectionString = "Data Source=MyServ;Initial Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=***;Application Name=MyApp";
    
    0 讨论(0)
提交回复
热议问题