【问题标题】:Only initializers, entity members, and entity navigation properties are supported. (ASP.NET MVC and Entity Framework)仅支持初始化程序、实体成员和实体导航属性。 (ASP.NET MVC 和实体框架)
【发布时间】:2011-10-08 21:20:39
【问题描述】:

我被困在我的 ASP.NET MVC 3 应用程序的某个地方。这是我得到的错误:

不支持指定的类型成员“AccommPropertyTags” LINQ 到实体。只有初始化器、实体成员和实体 支持导航属性。

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

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&lt;string&gt;。我在控制器中尝试的内容如下:

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,Entity 正试图将我的AccommPropertyTags 属性转换为 SQL 并且不能,因为它不是表架构的一部分。

我真的被困在这里,还是有一种很酷的方法可以解决这个烦人的错误?

【问题讨论】:

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


    【解决方案1】:

    您的问题与您链接的问题相似。在使用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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多