我卡在了我的 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/