无法创建类型(类型)的常量值在此上下文中仅支持基本类型(如Int32,String和Guid) [英] Unable to create a constant value of type (type) Only primitive types ('such as Int32, String, and Guid') are supported in this context

查看:222
本文介绍了无法创建类型(类型)的常量值在此上下文中仅支持基本类型(如Int32,String和Guid)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读全部:

实体框架 - 无法创建类型的常量值闭包类型...错误

Entity Framework - Union causes "Unable to create a constant value of type.."

在这种情况下,只支持原始类型(如Int32,String和Guid)。

Only primitive types ('such as Int32, String, and Guid') are supported in this context

并搜索更多,但仍然没有解决方案。我看到这发生在EF 3.5和4.0中,应该支持 Contains 方法,但是我在EF 4中,但是我收到这个错误。我有一个照片库,相册可以有多个不同的照片,每张照片可以属于任何数量的相册。所以这是一个多对多关系。

and searched a bit more, but still no solution. I've seen that this happens on EF 3.5 and in 4.0 the Contains method should be supported, but I'm in EF 4 but I'm getting this error. I have a photo gallery, where albums can have any number of different photos, and each photo can belong to any number of albums. So this is a many-to-many relationship.

我有一个 VisibleObjects 属性被使用约100种其他方法工作得很好,但我正在粘贴它:(我确定问题是不是由于这里的东西)

I've got a VisibleObjects property which is used by about 100 other methods that work well, but I'm pasting it anyway: (I'm definitely sure the problem is not caused by something here)

static IQueryable<GlobalObject> VisibleObjects
    {
        get
        {
            return from obj in db.GlobalObjectSet where obj.IsVisible && !obj.SiteUser.IsDeactivated orderby obj.ID descending select obj;
        }
    }

我尝试了几个不同的查询:

I've tried several different queries:

我有一个 VisiblePhotos 属性:

这不是工作:

static IQueryable<Photo> VisiblePhotos(this Album a)
    {
        return from p in VisibleObjects.OfType<Photo>() where a.Photos.Contains(p) select p;
    }

更改为:

static IQueryable<Photo> VisiblePhotos(this Album a)
    {
        return from p in VisibleObjects.OfType<Photo>() where a.Photos.Any(other => p.ID == other.ID) select p;
    }

仍然没有工作。

这是调用方法:

public static List<Photo> GetLatestPhotosByAlbum(Album alb, int count = 3)
    {
        lock (sync)
        {
            return alb.VisiblePhotos().OrderByDescending(p => p.ID).Take(count).ToList();
        }
    }

不工作,更改为: p>

Wasn't working, changed to this:

public static List<Photo> GetLatestPhotosByAlbum(Album alb, int count = 3)
    {
        lock (sync)
        {
            return (from p in VisibleObjects.OfType<Photo>()
                    where alb.Photos.Any(ph => ph.ID == ph.ID)
                    select p).ToList();
        }
    }

仍然没有工作。抱怨无法创建一个常量的我的照片对象类型,这是一个具有ID属性的实体对象,如果它有帮助。我不知道错误的真正原因,我没有任何其他的疑问想法。我认为方法名称是非常自我解释的:我试图在给定的专辑中获取照片。将相册条目加载到内存中不是解决方案,查询应该在数据库上运行,而不是内存。我需要解释这个异常,为什么它在这里发生,我该如何让我的查询工作。

Still isn't working. Complaining about not being able to create a constant of my Photo object type, which is an Entity object with an ID property, if it helps. I am not sure of the real cause of the error and I don't have any other ideas of queries in my mind. I think the method name is pretty self explanatory: I'm trying to get the photos in a given album. Loading album entries into memory is not a solution, the query should run on database, not memory. I need an explanation of this exception, why it is occuring here, and how can I get my query to work.

推荐答案

它不会工作,因为你想在linq-to-entities查询中使用本地相册。您必须使用p上的导航属性来获取其专辑:

It will not work because you want to use local Album in linq-to-entities query. You must either use navigation property on p to get its album:

var query = from p in VisibleObjects.OfType<Photo>()
            where p.Album.Id == alb.Id
            select p;

或者您必须使用照片和相册之间的一些连接构建复杂的查询。您不能传递本地对象及其与查询的任何关系。只能简单的属性可以传递。

or you must build complex query with some join between photos and albums. You cannot pass local object and any its relation to the query. Only simple properties can be passed.

这篇关于无法创建类型(类型)的常量值在此上下文中仅支持基本类型(如Int32,String和Guid)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆