我正在开展一个项目,我们使用 EF 进行数据访问,使用 WPF 和 MVVM 进行用户界面。
我正在将 View 模型绑定(bind)到窗口的数据上下文。这个 View 模型有一个默认的构造函数,定义如下:
public KonumVM()
{
LocationOperations = new LocationOperations();
LocationNames = new ObservableCollection<string>();
Corporations= new ObservableCollection<Kurum>();
//Corporations= LocationOperations.GetCorpValues();
//foreach (var corp in Corporations)
//{
// LocationNames.Add(corp.Name);
//}
}
注释行是我查询数据库的地方。如果这些行没有注释,当我尝试编辑绑定(bind)到此 View 模型的窗口的 XAML 代码时,我会遇到异常,我认为这是 XAML 加载失败,这会阻止设计器。异常在消息的末尾。当我评论数据库查询行时,设计者很好,没有异常(exception)。
我该怎么办?我应该将数据库访问代码移动到默认构造函数以外的其他地方吗?
Cannot open database "DemirbaşEntityLibrary.DemirbaşContext" requested by the login. The login failed. Login failed for user 'Rfid\Mert'.
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
最佳答案
遍历用于连接数据库的连接字符串。如果您的网站使用 MSSQL 数据库,请引用下面给出的示例连接字符串:
where Data Source=xxxx; = IP address of the server Initial Catalog=xxxxxx = database name User ID=xxxxxx = database user name Password=#xxxxx#" = db password
确保您能够使用数据库详细信息(用户名和密码)远程连接到 SQL Server Management Studio。
如果您无法使用您在连接字符串中使用的用户名和密码连接到数据库,请尝试重置数据库用户的密码并尝试连接它。它将解决问题...
关于c# - XAML 给出加载失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925125/