如何在代码中添加实体框架 6 提供程序?

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

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

问题描述

我在 C# 应用程序中使用 Entity Framework 6,它运行良好.创建模型时,会生成带有所有必要配置的 app.config.现在我不喜欢在 app.config 中有东西,所以我使用连接字符串生成器.我成功地从 app.config 文件中删除了除此之外的所有内容:

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>

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

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;
        }
    }

提前感谢您的帮助.

推荐答案

在 EF6 中,您可以使用 Code Base 配置.查看这篇文章了解更多详情.它显示了如何设置默认连接工厂(使用 SetDefaultConnectionFactory 方法).要设置提供程序,请使用 SetProviderServices 方法.

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 提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在代码中添加实体框架 6 提供程序?

基础教程推荐