使用带有自定义连接对象的 EF 存储库? [英] Using a EF Repository with custom connection objects?

查看:15
本文介绍了使用带有自定义连接对象的 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 存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆