Using a EF Repository with custom connection objects?(使用带有自定义连接对象的 EF 存储库?)
问题描述
我被困在 EF 6 并且文档很少 - 现在一天都没有解决.
I am stuck in EF 6 and the documentation is sparse - not getting that solved for a day now.
我尝试在我们拥有的数据库存储库上使用 Code First.由于复杂的初始化,我必须使用我自己的工厂方法来初始化上下文子类,并且我必须放入我自己的 sql 连接,或者制作我自己的工厂.
I try to use Code First on a database repository we have. Due to complex initialization I must use my own factory method to initialize the context subclass and I must put my own sql connection in, or make my own factory.
以下类初始化:
我们有:
public class Repository : DbContext {
static string _connectionString;
static Repository() {
Database.SetInitializer<Repository>(null);
var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
_connectionString = ** method to get connection string**
}
public static Repository Create() {
SqlConnection connection = new SqlConnection(_connectionString);
connection.Open();
connection.BeginTransaction(IsolationLevel.ReadCommitted).Commit();
return new Repository(connection);
}
不幸的是,在第一次尝试选择某个实体时运行它会出现以下异常:
Sadly running it blows with the following exception on the first attempt to select some entity:
无法确定System.Data.SqlClient.SqlClientFactory"类型的提供程序工厂的提供程序名称.确保在应用程序配置中安装或注册了 ADO.NET 提供程序.
Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
我完全不知道如何解决这个问题.
I am totally out of my mind how to fix that.
我在使用网络应用程序中的配置文件显示:
My config file in the using web application reads:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer, Version=6.0.0.0" />
</providers>
</entityFramework>
谁能告诉我如何解决这个问题?我的替代方案是先回到模型 - 但我真的很想先在这里尝试一下代码.
Anyone can tell me how to fix that? My alternative is to move back to model first - but I would really like to give code first a try here.
推荐答案
我对我的项目有相同的要求:EF 存储库必须接受从我自己的工厂创建的连接实例.为此,我选择完全忽略 EF 配置文件方案.相反,我做了以下事情:
I had the same requirements for my projects: EF repositories had to accept connection instances created from my own factory. To that end I chose to completely ignore the EF config file scheme. Instead, I did the following:
public class Repository : DbContext
{
static Repository ()
{
Database.SetInitializer<Repository>(null);
}
public Repository(SqlConnection existingConnection, bool contextOwnsConnection)
: base(existingConnection, contextOwnsConnection)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// creating the model myself
}
// (...)
}
由于它可以接受来自外部的连接实例,我可以随意使用我想要的任何连接工厂,并完全忽略 EF 的配置文件方案.
Since it can accept connection instances from the outside I'm free to use whatever connection factory I want and completely ignore EF's config file scheme.
如果您希望您的连接工厂位于存储库中(为了分离关注点,我不建议这样做),您可以在上面添加以下内容:
If you want your connection factory inside the repository (which I don't recommend for the sake of separating concerns), you could add the following to the above:
public Repository()
: base(CreateConnection(), true)
{
}
private static SqlConnection CreateConnection()
{
// do whatever you have to do to create your SqlConnection instance, using your own rules
}
这篇关于使用带有自定义连接对象的 EF 存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用带有自定义连接对象的 EF 存储库?
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01