EF6 / SQL Server Compact,基于代码的配置 [英] EF6/SQL Server Compact, code-based configuration
问题描述
我正在尝试将我的EF6配置从 myexe.exe.config
移动到代码,作为机器中空DbProviderFactories节点的解决方法。 config
-issue(在此描述: https://stackoverflow.com/a/24273922/600559)。我不想更改 machine.config
文件。
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.
我已经阅读了基于代码的配置(EF6以上)。
我已经尝试过这样的实现: https://stackoverflow.com/a/23130602/600559 ,但是我不能让它工作。有没有人有一个工作的EF6 / SQL CE /基于代码的配置解决方案?
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?
这是我的更改(从工作的.config解决方案到基于代码的解决方案):
添加新类:
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);
}
}
然后 system.data
和 entityFramework
节点在 .config
-file中被删除。
Then the system.data
and entityFramework
node in the .config
-file is removed.
现在这样做,但是 machine.config
文件被读取:如果一个有< DbProviderFactories / < / code> in
machine.config
我得到这个例外:
Now this works, however the machine.config
files is read: If a have the <DbProviderFactories/>
in machine.config
I get this exception:
所以真正的问题不在于基于代码的配置没有工作,问题是 machine.config
配置仍然被读取并导致问题。任何人都知道如何解决这个问题?
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?
推荐答案
找到一个解决方案。实现不从 machine.config
文件中读取的 IDbProviderFactoryResolver
:
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;
}
}
这篇关于EF6 / SQL Server Compact,基于代码的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!