2016-06-10 68 views
0
當我解決出現InvalidOperationException

查詢:如何查詢EF代碼第一個數據庫

var user = db.Users.FirstOrDefault(u => u.Email.Equals(username) && u.Password.Equals(password)); 

錯誤:

Local = 'db.Users.Local' threw an exception of type 'System.InvalidOperationException' 

詳細的錯誤信息:

Message = "The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of D... Source: ""entityframework" StackTrace:StackTrace = " at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()\r\n at System.Data.Entity.Internal.InternalContext.Initialize()\r\n at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\r\n at System...

+0

連接之前,你在哪裏把你的代碼?查詢沒什麼問題,但我猜你正在把它放在一個錯誤的地方。 – akazemis

+0

副節點:將密碼存儲在數據庫中是不好的做法;存儲密碼的密碼哈希,最好是SALT。 – N8allan

+0

事情是它工作正常,直到我將我的項目從我的文檔移動到c:驅動器。我不知道這是否可能導致了這個問題。 –

回答

0

您可以設置MultipleActiveResultSets=true;在您的連接字符串

您應該注意以下錯誤消息:

o r if the same context instance is accessed by multiple threads同時發生。

yourcontext context = new yourcontext(); 

var user = context.Users.FirstOrDefault(u => u.Email.Equals(username) && u.Password.Equals(password)); 

這裏使用的情況下,你必須處理像

相關問題