EF6/SQL Server Compact, code-based configuration

好久不见. 提交于 2019-12-03 22:50:08

问题


I'm trying to move my EF6 configuration from myexe.exe.config to code as a workaround for the empty DbProviderFactories node in machine.config-issue (described here: https://stackoverflow.com/a/24273922/600559). I don't want to change the machine.config file.

I have read the Code-Based Configuration (EF6 onwards).

I have tried implementations like this: https://stackoverflow.com/a/23130602/600559, however I cannot get it to work. Does anyone have a working EF6/SQL CE/code-based configuration solution?

Here's my my changes (from a working .config solution to a code based solution): New class added:

public class DatabaseConfiguration : DbConfiguration
{
    public DatabaseConfiguration()
    {
        SetExecutionStrategy("System.Data.SqlServerCe.4.0", () => new DefaultExecutionStrategy());
        SetProviderFactory("System.Data.SqlServerCe.4.0", new SqlCeProviderFactory());
        SetProviderServices("System.Data.SqlServerCe.4.0", SqlCeProviderServices.Instance);
    }
}

Then the system.data and entityFramework node in the .config-file is removed.

Now this works, however the machine.config files is read: If a have the <DbProviderFactories/> in machine.config I get this exception:

So the real problem is not that the code based configuration does not work, the problem is that the machine.config configuration is still being read and causes issues. Anyone knows how to solve this?


回答1:


Found a solution. Implementing a IDbProviderFactoryResolver that does not read from the machine.config file:

  public class CodeBasedDatabaseConfiguration : DbConfiguration
  {
    public CodeBasedDatabaseConfiguration()
    {
      SetExecutionStrategy("System.Data.SqlServerCe.4.0", () => new DefaultExecutionStrategy());
      SetProviderFactory("System.Data.SqlServerCe.4.0", new SqlCeProviderFactory());
      SetProviderServices("System.Data.SqlServerCe.4.0", SqlCeProviderServices.Instance);
      SetProviderFactoryResolver(new CodeBasedDbProviderFactoryResolver());
    }
  }

  internal class CodeBasedDbProviderFactoryResolver : IDbProviderFactoryResolver
  {
    private readonly DbProviderFactory sqlServerCeDbProviderFactory = new SqlCeProviderFactory();

    public DbProviderFactory ResolveProviderFactory(DbConnection connection)
    {
      var connectionType = connection.GetType();
      var assembly = connectionType.Assembly;
      if (assembly.FullName.Contains("System.Data.SqlServerCe"))
      {
        return sqlServerCeDbProviderFactory;
      }
      if (assembly.FullName.Contains("EntityFramework"))
      {
        return EntityProviderFactory.Instance;
      }
      return null;
    }
  }


来源:https://stackoverflow.com/questions/29682096/ef6-sql-server-compact-code-based-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!