更改实体连接字符串错误

时间:2012-01-04 09:14:19

标签: asp.net linq linq-to-entities connection-string invalidoperationexception

我在更改模型实体connection string时遇到问题 用户首次登录时正确选择KurtDBEntities conection string 但是当以管理员if (daRoles.IsUserInRole(User, 1))或任何其他实例登录时,它会给我这个错误:

System.InvalidOperationException was unhandled by user code
  Message=No modifications to connection are permitted after the metadata has been registered either by opening a connection or constructing the connection with a MetadataWorkspace.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.EntityClient.EntityConnection.ValidateChangesPermitted()
       at System.Data.EntityClient.EntityConnection.set_ConnectionString(String value)
       at DataLayer.ConnectionClass..ctor(String User) in Documents\Visual Studio 2010\Projects\DataLayer\ConnectionClass.cs:line 32
       at BusinessLayer.BLBase..ctor(String user) in Documents\Visual Studio 2010\Projects\BusinessLayer\BLBase.cs:line 54
       at BusinessLayer.Roles..ctor(String userLogged) in Visual Studio 2010\Projects\BusinessLayer\Roles.cs:line 12
       at PresentationLayer.UserControls.Menu.Page_Load(Object sender, EventArgs e) in C:\Users\Documents\Visual Studio 2010\Projects\PresentationLayer\UserControls\Menu.ascx.cs:line 23
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

public ConnectionClass(string User)
        {

            this.Entities = new KurtDBEntities();
            DataLayer.DARoles daRoles = new DARoles(this.Entities);
            if (User == "Login")

        {
            this.Entities.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["KurtDBEntities"].ConnectionString.ToString();
        }
        else
        {
            //can also use entity connection string builder

            if (User != "")
            {
                if (daRoles.IsUserInRole(User, 1))
                {

                    this.Entities.Connection.ConnectionString = @"Data Source=KURT-PC\SQLEXPRESS;Initial Catalog=KurtDB;User ID=Admin;Password=123456";
                }
                else if (daRoles.IsUserInRole(User, 2))
                {
                    this.Entities.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["BasicUser"].ConnectionString.ToString();
                }

            }
            else
            {
                this.Entities.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["KurtDBEntities"].ConnectionString.ToString();
            }

        }


        //this.Entities.Connection.ConnectionString = conn;
    }

}

请帮助吗?

1 个答案:

答案 0 :(得分:2)

请参阅my answer to your other question

您需要首先确定要使用的连接字符串,然后才能新建Entities对象:

string connectionString = GetConnectionStringBasedOnUserRole(); // do your magic here
this.Entities = new KurtDBEntities(connectionString);

在此之后,您应该能够正常使用实体,指向正确的数据库/服务器。