5

我被困在我的ASP.NET MVC 3應用程序的某個地方。這裏是我得到的錯誤:僅支持初始化程序,實體成員和實體導航屬性。 (ASP.NET MVC和實體框架)

指定的類型成員「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<string>。什麼我正控制器內部如下:

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並且不能因爲它不是表模式的一部分。

我真的陷入這裏,還是有一種很酷的方式來擊敗這個惱人的錯誤?

回答

10

您的問題與您鏈接的問題類似。在使用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); 
}