2016-06-10 71 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)); 

这里使用的情况下,你必须处理像

相关问题