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

查看:173
本文介绍了使用EF库自定义的连接对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在EF 6和文档是稀疏的 - 没有得到这对于解决现在一天

I am stuck in EF 6 and the documentation is sparse - not getting that solved for a day now.

我尝试使用code首先在数据库存储库,我们有。由于复杂的初始化我的必须的用我自己的工厂方法来初始化上下文子和我的必须的把我自己的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.

在使用Web应用程序我的配置文件内容如下:

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>

任何人都可以告诉我怎么解决这个问题?我的选择是回迁第一个模型 - 但我真的想给code第一个尝试此处

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天全站免登陆