开发者

Linq to Entities - Query errors in Web but not Unit tests

开发者 https://www.devze.com 2023-01-22 14:15 出处:网络
I have a query that errors out when I use it in a MVC 2.0 project, but not when I run unit tests against it (works fine).Below is the query, \"User\" is an entity.The error I get shows up on the Known

I have a query that errors out when I use it in a MVC 2.0 project, but not when I run unit tests against it (works fine). Below is the query, "User" is an entity. The error I get shows up on the Known issues to consider for EF (http://msdn.microsoft.com/en-us/library/bb896317.aspx) but it seems pretty basic this should work, and I'm not really doing what they are doing. This is targeting the 3.5 framework and sql 2005 backend.

The error is

"Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."

The query is

User user = (from u in db.Users
                where u.UserId == (Guid)providerUserKey
                && u.Application.LoweredApplicationName == this.ApplicationName.ToLower()
                select u).FirstOrDefault();

I was able to determine that the query fails because of selecting "User". The same query however works perfectly fine when I 开发者_如何学JAVAuse u.UserName = userName. I have also tried variations like UserId.ToString() (also not supported) and u.UserId.CompareTo((Guid)providerKey) > 0. All throw errors of some type.

Any ideas?

Thanks!


From this post, it looks like the problem is with providerUserKey, not with u.UserId.

Try assigning providerUserKey to a Guid, and using that:

Guid id = (Guid)providerUserKey;

User user = (from u in db.Users
                where u.UserId == id
                && u.Application.LoweredApplicationName == this.ApplicationName.ToLower()
                select u).FirstOrDefault();
0

精彩评论

暂无评论...
验证码 换一张
取 消