[英]LocalDb error when accessing data “The underlying provider failed on Open”
我目前正嘗試連接到數據庫的本地副本以進行測試,以免更改實時數據。
我將MVC與Entity Framework(.Net 4.5)結合使用,並且每當嘗試通過EF訪問數據時,我都遇到此錯誤:
An exception of type 'System.Data.EntityException' occurred in System.Data.Entity.dll but was not handled in user code
Additional information: The underlying provider failed on Open.
這是嘗試訪問數據的功能-錯誤發生在第一行
public IEnumerable<Client> GetClients()
{
List<Client> clients = (from c in this.repository.Clients select c).ToList();
return clients;
}
這也是我在app.config中的連接字符串:
<add name="EntitiesConnectionString" connectionString="metadata=res://*/EFName.csdl|res://*/EFName.ssdl|res://*/EFName.msl;provider=System.Data.SqlClient;provider connection string="Data Source=(LocalDb)\v11.0;Initial Catalog=testdb;Integrated Security=True;Pooling=False"" providerName="System.Data.EntityClient" />
我正在使用Visual Studio 2015隨附的集成SQL Server Express來管理我創建的本地數據庫。
我發現的更多信息:
"Cannot open database \"testdb\" requested by the login. The login failed.\r\nLogin failed for user 'NT AUTHORITY\\SYSTEM'."
我不相信我在創建的數據庫上設置了任何權限,也許連接字符串有問題?
編輯:
我添加了“ NT AUTHORITY \\ NETWORK SERVICE”用戶,然后將其添加到MS SQL Server Management Studio中的“ db_owner”角色中,但沒有任何運氣
我還嘗試使用憑證和“ db_owner”角色創建一個新的登錄名和用戶,但仍然收到相同的“用戶'user'登錄失敗”的信息。
這是我與創建的測試用戶一起使用的連接字符串:
<add name="EntitiesConnectionString" connectionString="metadata=res://*/EFName.csdl|res://*/EFName.ssdl|res://*/EFName.msl;provider=System.Data.SqlClient;provider connection string="Data Source=(LocalDb)\v11.0;Initial Catalog=testdb;Integrated Security=False;Persist Security Info=True;User ID=test;Password=password"" providerName="System.Data.EntityClient" />
處置基礎連接后,您可能會枚舉GetClients()
。
嘗試以下任一方法:
a)將方法的返回類型更改為類似List< Client>
public List<Client> GetClients()
{
List<Client> clients = (from c in this.repository.Clients select c).ToList();
return clients;
}
或b)立即在方法上調用.ToList()
:
IEnumerable<Clients> clients = GetClients().ToList();
.ToList()
在您的方法中延遲執行.ToList()
直到枚舉該方法的返回值為止,如果我不得不猜測,這是在數據庫連接被.ToList()
之后。
作為返回IEnumerable<Client>
您還必須返回Enumerable<Client>
。 使用如下
public IEnumerable<Client> GetClients()
{
List<Client> clients = (from c in this.repository.Clients select c).ToList();
return clients.AsEnumerable();
}
希望對你有幫助
為什么添加了NT AUTHORITY \\ NETWORK SERVICE? 從第一條錯誤消息中,您看起來正在運行的任何上下文都是NT AUTHORITY \\ SYSTEM用戶。
如果是這種情況,我認為您需要添加:NT AUTHORITY \\ SYSTEM作為數據庫登錄名,並為其指定db_owner或所需的任何適當角色。
如果那不能解決問題,我可以再看一遍。
我找到了解決我問題的方法! 有一些事情需要完成。
首先,以下服務未運行,需要運行:
SQL Server(MSSQLSERVER)
SQL Server代理(MSSQLSERVER)
SQL Server瀏覽器
其次,我使用了不同的本地服務器, 而不是(LocalDb)\\ v11.0或(LocalDb)\\ mssqllocaldb,然后將數據庫和數據重新復制到該服務器(僅使用計算機的名稱作為數據源)
第三,我必須通過SQL Server Management Studio連接到本地數據庫,並創建具有db_owner權限的登錄名/密碼/用戶,然后按如下方式在我的連接字符串中使用它們:
<add name="EntitiesConnectionString" connectionString="metadata=res://*/EFName.csdl|res://*/EFName.ssdl|res://*/EFName.msl;provider=System.Data.SqlClient;provider connection string="Data Source=POI;Initial Catalog=testdb;Integrated Security=False;Persist Security Info=True;User ID=test_user;Password=thepassword"" providerName="System.Data.EntityClient" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.