Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在代码中添加实体框架6提供程序?_C#_Connection String_Entity Framework 6 - Fatal编程技术网

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

C# 如何在代码中添加实体框架6提供程序?,c#,connection-string,entity-framework-6,C#,Connection String,Entity Framework 6,我在C#应用程序中使用EntityFramework6,它工作得非常好。创建模型时,将生成app.config,其中包含所有必要的配置。现在我不喜欢app.config中的东西,所以我使用连接字符串生成器。我成功地从app.config文件中删除了所有内容,除了以下内容: <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFac

我在C#应用程序中使用EntityFramework6,它工作得非常好。创建模型时,将生成app.config,其中包含所有必要的配置。现在我不喜欢app.config中的东西,所以我使用连接字符串生成器。我成功地从app.config文件中删除了所有内容,除了以下内容:

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

提前感谢您的帮助。

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

您将他链接到了他已经提到的msdn上的一篇文章。另外,那篇文章没有告诉您如何创建sql连接字符串。@Bob-我不知道我是否错过了链接,或者在添加答案后,问题是否被链接更新了。有趣的是,我所指的文章实际上包含了一个片段,展示了如何设置默认连接工厂。不管怎样,我对答案做了一点改进,并给出了一些建议,这些建议应该可以使解决问题变得更容易。@Pawel+1您的答案是正确的,代码在您提供的链接的一半,事实上允许他的App.config不受限制。答案在您提供的链接中
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;
        }
    }