如何使用基于代码的配置在Entity Framework 6中设置连接 [英] How to set connection in Entity Framework 6 using Code-Based Configuration

查看:209
本文介绍了如何使用基于代码的配置在Entity Framework 6中设置连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对有关基于EF6代码的配置的(稀疏)MSDN文档感到困惑。我有一个WPF应用程序,需要根据命令行参数和/或从对话框的用户输入创建一个SQL连接。在旧版本的EF中,我通过构建连接字符串并将其传递给每个派生的DbContext来实现。



如果我可以避免这样做,因为它也意味着必须将连接字符串传递给需要使用上下文的每个视图模型。看来EF6中的DbConfig正在实现这一目标。根据文档,我创建了一个简单的自定义类:

  public class EfConfiguration:DbConfiguration 
{
public EfConfiguration()
{
SetExecutionStrategy(System.Data.SqlClient,()=> new SqlAzureExecutionStrategy());
SetDefaultConnectionFactory(new SqlConnectionFactory());
SetDatabaseInitializer< DataContext>(null);
}
}

根据我的理解,EF会收拾与...有关但我的问题是,一旦应用程序运行,我该如何设置连接字符串?在这样做的时候,这是否意味着我现在可以实例化没有参数的DbContext?

解决方案

问题,这是我如何解决它:



DbContext有一个接受现有连接的构造函数。因此,我的派生DbContext(在这种情况下称为实体)具有一个构造函数,如下所示:

  public Entities(IConnectionHelper connHelper)
:base(connHelper.GetConnection(),true)
{}

IConnectionHelper是一个注入的单例,具有服务器/ db / uname / pw的属性,并且在更改它们时,使用SqlConnectionStringBuilder构建内部连接字符串。由于它是一个单例,用户可以从对话框修改属性,例如,它们仍然可以在其他地方访问。 GetConnection()方法从工厂创建一个连接:

  SqlConnectionFactory connFactory = new SqlConnectionFactory(_connectionString); 
return connFactory.CreateConnection(_connectionSettings.Database);

其中_connectionString只是我构建的字符串,_connectionSettings只是一个保存各种连接参数的POCO。



然后为了实现DbContext的无参数构造的目标,我做了一个简单的工厂,它将IConnectionHelper构造函数注入为依赖关系:

  public EntitiesFactory(IConnectionHelper connHelper)
{
_connHelper = connHelper;
}

public Entities Create()
{
return new Entities(_connHelper);
}

我将这个工厂注入到需要它的任何类中,然后是一个简单的事项:

  using(var db = _entitiesFactory.Create())
{...}

您也可以使工厂方法静态,而不用担心注入,但是我这样做是为了方便测试。


I'm a bit confused by the (sparse) MSDN documentation on EF6 code-based configuration. I have a WPF application that needs to create an SQL connection based on command-line parameters and/or user input from a dialog. In older versions of EF I accomplished this by building the connection string and passing it to my derived DbContext every time.

It would be nice if I could avoid having to do that, since it also means having to pass the connection string to every view model that needs to use the context. It seems like the DbConfig in EF6 is on the path to accomplish that. Based on the docs, I have created a custom class that simply looks like this:

public class EfConfiguration: DbConfiguration
{
    public EfConfiguration() 
    { 
        SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy()); 
        SetDefaultConnectionFactory(new SqlConnectionFactory());
        SetDatabaseInitializer<DataContext>(null);
    } 
}

Which, from my understanding, EF will pickup and do something with. But my question is, how do I then set the connection string once the application is running? And in doing so, does that mean I can now instantiate my DbContext with no parameters?

解决方案

In case anyone else runs across this problem, here's how I solved it:

DbContext has a constructor that accepts an existing connection. So my derived DbContext (called Entities in this case) has a constructor which looks like:

    public Entities(IConnectionHelper connHelper)
        :base(connHelper.GetConnection(), true)
    { }

The IConnectionHelper is an injected singleton that has properties for the server/db/uname/pw and upon changing them, builds an internal connection string using SqlConnectionStringBuilder. Since it's a singleton, the user can modify the properties from a dialog, for example, and they will still be accessible anywhere else. The GetConnection() method creates a connection from the factory:

            SqlConnectionFactory connFactory = new SqlConnectionFactory(_connectionString);
            return connFactory.CreateConnection(_connectionSettings.Database);

Where _connectionString is just the string I built and _connectionSettings is just a POCO that holds the various connection parameters.

Then to achieve the goal of having a parameterless construction of the DbContext, I made a simple factory which gets the IConnectionHelper constructor-injected as a dependency:

    public EntitiesFactory(IConnectionHelper connHelper)
    {
        _connHelper = connHelper;
    }

    public Entities Create()
    {
        return new Entities(_connHelper);
    }

I inject this factory into any classes that need it, and then it's a simple matter of:

using (var db = _entitiesFactory.Create())
{ ... }

You could also make the factory method static and not worry about injecting, but I did it this way to facilitate testing.

这篇关于如何使用基于代码的配置在Entity Framework 6中设置连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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