c# - XAML 给出加载失败错误

标签 c# wpf entity-framework xaml mvvm

我正在开展一个项目,我们使用 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。

如果您无法使用您在连接字符串中使用的用户名和密码连接到数据库,请尝试重置数据库用户的密码并尝试连接它。它将解决问题...

您还可以查看以下可能对您有帮助的链接:http://blog.sqlauthority.com/2009/08/20/sql-server-fix-error-cannot-open-database-requested-by-the-login-the-login-failed-login-failed-for-user-nt-authoritynetwork-service/

关于c# - XAML 给出加载失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925125/

相关文章:

c# - 如何通过 "Microsoft print to PDF"以编程方式将文件和网页打印为 PDF?

c# - 无法将 JSON 数组(例如 [1,2,3])反序列化为类型 ' ',因为类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化

c# - 设置不可调整大小的 GridViewColumn

entity-framework - 获取 Entity Framework 原始查询以尊重属性

sql - Entity Framework 为一列选择具有最大值的行

c# - 限制文本日志文件的好方法

c# - WPF 设计 : How to make User control with rounded corner and shadow effect

c# - 从字节数组创建 BitmapImage

c# - 按钮样式在运行时有效,但 Visual Studio 在设计时显示错误

c# - Cast() 异常 : LINQ to Entities only supports casting EDM primitive or enumeration types