如何在代码中添加实体框架6提供程序? [英] How to add entity framework 6 provider in code?

查看:155
本文介绍了如何在代码中添加实体框架6提供程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#应用程序中使用Entity Framework 6,它的工作原理很好。创建模型时,将使用所有必要的配置生成app.config。现在我不喜欢在app.config中有东西,所以我使用连接字符串构建器。我成功地删除了app.config文件中的所有内容,除了以下内容:

 < entityFramework> 
< defaultConnectionFactory type =System.Data.Entity.Infrastructure.SqlConnectionFactory,EntityFramework/>
< providers>
< provider invariantName =System.Data.SqlClienttype =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< / providers>
< / entityFramework>

如果我删除它,它不工作。那么如何将该配置转换为c#代码呢?
我该怎么办?我查看了基于代码的配置( http://msdn.microsoft.com/ en-us / data / jj680699 )但是没有帮助。



我创建连接字符串的部分类似于:

  public partial class LogEntities 
{
public LogEntities(string serverName)
:base(GetConnectionString(serverName) )
{
}

public static string GetConnectionString(string serverName)
{
//指定提供程序名称,服务器和数据库。
const string databaseName =_LOG;

//初始化底层提供程序的连接字符串构建器。
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder
{
DataSource = serverName,
InitialCatalog = databaseName,
IntegratedSecurity = true,
MultipleActiveResultSets = true
} ;

//初始化EntityConnectionStringBuilder。
System.Data.EntityClient.EntityConnectionStringBuilder entityBuilder = new System.Data.EntityClient.EntityConnectionStringBuilder();
entityBuilder.Provider =System.Data.SqlClient;

//设置特定于提供者的连接字符串。
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();

//设置元数据位置。
entityBuilder.Metadata = @res://*/myproject.LogModel.csdl | res://*/myproject.LogModel.ssdl | res://*/myproject.LogModel.msl;

return entityBuilder.ConnectionString;
}
}

提前感谢任何帮助。

解决方案

在EF6中,您可以使用代码库配置。有关详细信息,请参阅文章。它显示如何设置默认连接工厂(使用 SetDefaultConnectionFactory 方法)。要设置提供者,请使用 SetProviderServices 方法。


I use Entity Framework 6 in a C# application and it works perfectly. When creating the Model, the app.config is generated with all the necessary configuration. Now I don't like having stuff in the app.config, so I use the connection string builder. I succeeded in removing everything out of the app.config file except of this:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

If I remove this, it does not work. So how can I translate that config into c# code? How to I do that? I've looked at the code-based configuration (http://msdn.microsoft.com/en-us/data/jj680699) however, it did not help.

My partial class that creates the connection string looks like that:

public partial class LogEntities
    {
        public LogEntities(string serverName)
            : base(GetConnectionString(serverName))
        {
        }

        public static string GetConnectionString(string serverName)
        {
            // Specify the provider name, server and database.
            const string databaseName = "_LOG";

            // Initialize the connection string builder for the underlying provider.
            SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder
            {
                DataSource = serverName,
                InitialCatalog = databaseName,
                IntegratedSecurity = true,
                MultipleActiveResultSets = true
            };

            // Initialize the EntityConnectionStringBuilder.
            System.Data.EntityClient.EntityConnectionStringBuilder entityBuilder = new System.Data.EntityClient.EntityConnectionStringBuilder();
            entityBuilder.Provider = "System.Data.SqlClient";

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = sqlBuilder.ToString();

            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/myproject.LogModel.csdl|res://*/myproject.LogModel.ssdl|res://*/myproject.LogModel.msl";

            return entityBuilder.ConnectionString;
        }
    }

Thanks in advance for any help.

解决方案

In EF6 you can use Code Base configuration. Take a look at this article for more details. It shows how to set the default connection factory (use the SetDefaultConnectionFactory method). To set the provider you use the SetProviderServices method.

这篇关于如何在代码中添加实体框架6提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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