c# - 通过代码设置实体连接字符串

标签 c# winforms entity-framework

好的,所以我对切换数据库的实现有了更进一步的了解,我的上下文类中有一个方法。

public string SetupConnections()
{

        //Specify the provider name, server and database.
string providerName = "System.Data.SqlClient";
        string serverName = ".";
        string databaseName = "DBNAME";

        // Initialize the connection string builder for the
        // underlying provider.
        SqlConnectionStringBuilder sqlBuilder =
            new SqlConnectionStringBuilder();

        // Set the properties for the data source.
        sqlBuilder.DataSource = serverName;
        sqlBuilder.InitialCatalog = databaseName;
        sqlBuilder.IntegratedSecurity = false;
        sqlBuilder.UserID = "USERNAME";
        sqlBuilder.Password = "PASS";

        // Build the SqlConnection connection string.
        string providerString = sqlBuilder.ToString();

        // Initialize the EntityConnectionStringBuilder.
        EntityConnectionStringBuilder entityBuilder =
            new EntityConnectionStringBuilder();

        //Set the provider name.
        entityBuilder.Provider = providerName;

        // Set the provider-specific connection string.
        entityBuilder.ProviderConnectionString = providerString;

        // Set the Metadata location.
        entityBuilder.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";
        Console.WriteLine(entityBuilder.ToString());

        using (EntityConnection conn =
            new EntityConnection(entityBuilder.ToString()))
        {
            conn.Open();
            Console.WriteLine("Just testing the connection.");
            conn.Close();
        }
        return sqlBuilder.ToString();
    }

但是当我运行该应用程序时,它说底层提供程序无法为用户打开,我是否添加了正确的用户名和密码?出于安全原因,obv 伪装了我的真实密码。

这是我的原始连接字符串。我在 technet 上关注这个例子

https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/ef/how-to-build-an-entityconnection-connection-string

是的,我没有那个例子是集成的 security = true 但我不想使用我想使用的用户名和密码。

 <add name="SMBASchedulerEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=sourcenet;initial catalog=COASTALCAROLINAPODIATRY;User ID=scheduler;Password=Pass;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

我也遇到了这个错误,因为我正在使用 DB First

: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

我没有得到任何数据,或者我将我的实体对象更改为如下。

    public SMBASchedulerEntities(string sConnectionString)
        : base(sConnectionString)
    {

    }

最佳答案

您正在为 entityBuilder.Provider 提供“System.Data.SqlClient”,但据我所知,它应该是“System.Data.EntityClient”以使用实体生成器创建连接字符串。

关于c# - 通过代码设置实体连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48055315/

相关文章:

c# - 为什么在 DataGridView 控件中删除行时出现此错误?

c# - 在所有屏幕上克隆窗口

c# - 更改 tabControl 的 Tab 大小

c# - 在 ef 中将对象列表添加到上下文

c# - wp7 xdocument xml节点c#

c# - 使用 WSDL Web 服务时底层连接已关闭

c# - 在 C# 中运行时取消选中复选框时禁用文本框

c# - 使用 Entity Framework 数据模型从表中获取多个数据

sql - 什么时候使用 .mdf 什么时候使用 .sdf?

c# - WPF 找不到资源