0

我在更改我的实体连接字符串时遇到了这个问题。

这是代码:

SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
                        sqlBuilder.DataSource = @"KURT-PC\SQLEXPRESS";
                        sqlBuilder.InitialCatalog = "KurtDB";
                        sqlBuilder.UserID = "Admin";
                        sqlBuilder.Password = "123456";
                        sqlBuilder.IntegratedSecurity = false;
                        sqlBuilder.MultipleActiveResultSets = true;

                        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
                        entityBuilder.Provider = "System.Data.SqlClient";
                        entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
                        entityBuilder.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";

                        this.Entities.Connection.ConnectionString = conn.Connection.ConnectionString = entityBuilder.ConnectionString.ToString();

当它尝试更改连接字符串时,它给了我一个 InvalidOperationEsception。

错误详细信息:通过打开连接或使用 MetadataWorkspace 构建连接来注册元数据后,不允许对连接进行修改。

任何帮助它让我无言以对....

4

1 回答 1

4

我假设在您的示例中,this.Entities是实体框架对象上下文的实例。

错误消息意味着您不能简单地将新的连接字符串分配给对象的Connection属性Entities

相反,尝试将其作为构造函数参数传递:

  • 找到您实例化实体的行:
    this.Entities = new ...();
  • 把它改成这样:
    this.Entities = new ...(entityBuilder.ConnectionString.ToString());
于 2012-01-05T22:29:56.313 回答