Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

c# - Configure multiple database Entity Framework 6

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 solution needs to use both databases. My problem is how to configure those context. I tried to create a configuration class in a separate assembly:

namespace OSAD_Base
{
    class EfDbConfiguration : DbConfiguration
    {
        public EfDbConfiguration()
        {
            SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
        }
    }
}

and referencing to this configuration in each context class:

namespace IntegrationDb
{
    [DbConfigurationType("OSAD_Base.EfDbConfiguration, OSAD_Base")]
    public partial class IntegrationEntities : DbContext
    {
        public IntegrationEntities(string connectionString)
            : base(connectionString)
        {
        }
    }
}

When initializing my first, all works correct, but when the second context initializes (Order does not matter) I get and error:

An instance of 'EfDbConfiguration' was set but this type was not discovered in the same assembly as the 'B1Entities' context. Either put the DbConfiguration type in the same assembly as the DbContext type, use DbConfigurationTypeAttribute on the DbContext type to specify the DbConfiguration type, or set the DbConfiguration type in the config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.*

I also tried to create an entityframework section in my app.config (of the start up project) but got the following error:

Configuration system failed to initialize

Unrecognized configuration section entityFramework

How can I use 2 separate EF Projects in the same solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...