c# - 仅支持初始值设定项、实体成员和实体导航属性。 (ASP.NET MVC 和 Entity Framework )

标签 c# asp.net-mvc asp.net-mvc-3 entity-framework entity-framework-4.1

我卡在了我的 ASP.NET MVC 3 应用程序的某个地方。这是我收到的错误:

The specified type member 'AccommPropertyTags' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

我在下面的文章中找到了解决这个问题的方法:

Only initializers, entity members, and entity navigation properties are supported

但是我的有点奇怪。

这是我实体的部分类之一:

[MetadataType(typeof(AccommPropertyWebDetail.MetaData))]
public partial class AccommPropertyWebDetail {

    public virtual ICollection<string> AccommPropertyTags {

        get {

            return Helpers.AccommPropertyTag.CreateStringListFromString(this.PropertyTags);
        }
    }

    private class MetaData {

        [Required, StringLength(50)]
        public string Category { get; set; }

    }
}

如您所见,AccommPropertyTags属性是 typeof ICollection<string> .我在 Controller 中尝试的内容如下:

public ViewResult Tag(string tag) {

    var _rawTag = HttpUtility.UrlDecode(tag);

    ViewBag.Tag = _rawTag;

    var model = _accommpropertyrepo.GetAllAccommPropertiesFullWebIgnoringApprovalStatus();

    model = model.Where(
            x => x.AccommPropertyTags.Any(
                    y => y == _rawTag
                )
        );

    return View(model);
}

因为我使用的是 Any在那里,实体正试图转换我的 AccommPropertyTags属性到 SQL 并且不能,因为它不是表架构的一部分。

我真的被困在这里了吗?还是有一种很酷的方法来克服这个恼人的错误?

最佳答案

您的问题与您链接的问题类似。在使用 Where 之前调用 model.ToList()。这将强制 EF 实体化实体,然后在内存中应用其余的过滤。

public ViewResult Tag(string tag) {

    var _rawTag = HttpUtility.UrlDecode(tag);

    ViewBag.Tag = _rawTag;

    var model = _accommpropertyrepo.GetAllAccommPropertiesFullWebIgnoringApprovalStatus();

    var result = model.ToList().Where(
            x => x.AccommPropertyTags.Any(
                    y => y == _rawTag
                )
        );

    return View(result);
}

关于c# - 仅支持初始值设定项、实体成员和实体导航属性。 (ASP.NET MVC 和 Entity Framework ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7696977/

相关文章:

asp.net-mvc - 每个客户的 Asp MVC 路由

c# - 无需序列化或填充即可将对象转换为字节数组

c# - 如何转换可能为空的 bool 值

c# - 如何从 Windows 窗体 DateTimePicker 控件复制日期值?

c# - Visual Studio 2012 Express 对读取大文件有限制吗?

c# - 在vs2012中设置为起始页

templates - 未呈现基类模板的嵌套@ Html.DisplayFor(model => baseClass, "BaseClass")

c# - ASP.NET MVC 应用程序的版本控制 REST API

c# - ASP.NET 中的全局变量

c# - C#是否具有用于将JSON文本转换为可管理对象的内置对象,还是我需要第三方库?