我正在使用此 linq 查询进行登录
var login = context.Person_Login
.Where(c => c.Username == username && c.Password == password)
.DefaultIfEmpty(new Person_Login({Id = -1})
.First();
但是在执行中抛出这个异常:
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll
Additional information: Unable to create a constant value of type 'MyProject.MyModels.Person_Login'. Only primitive types or enumeration types are supported in this context.
最佳答案
异常消息非常具有描述性。
DefaultIfEmpty(new Person_Login({Id = -1}))
在 Linq to Entities 中不受支持。
你可以用下面的代替
var login = context.Person_Login
.FirstOrDefault(c => c.Username == username && c.Password == password)
?? new Person_Login {Id = -1};
请注意,DefaultIfEmpty
方法主要用于执行 LINQ 左外连接。
关于C#-Linq : Unable to create a constant value of type Only primitive types or enumeration types are supported in this context.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34415910/