2012-09-24 28 views
0

我一直在使用ddd模式的實體框架項目。我不使用實體的默認自定義工具,我有存儲庫和一些模型類。無法在實體上設置字段/屬性

我有我的實體框架3個表:

  1. 類別
  2. CategoryContent
  3. 內容

分類與categoryContent和內容categoryContent有1對多的關係。 (我不使用多對多這裏,是因爲我有CategoryContent表中的某些屬性)

當我發送的請求CategoryContent的列表,顯示content.Title這個名單MVC中我看到了這個錯誤:

Unable to set field/property CategoryContents on entity type System.Data.Entity.DynamicProxies.Content_731FEFC3E064D6098DC133918117C2359E2F308D2EB1E5D8D986538641AAB7CD.

但是當我追查時,程序沒有任何問題並且返回數據並顯示。

我使用消息系統(服務項目中的請求和響應消息)。

此代碼是contentcategory的返回列表的一種方法。

public IEnumerable<CategoryContent> GetContents(int categoryID) 
    { 
     Query query = new Query(); 
     OrderByClause order = new OrderByClause(); 
     order.PropertyName = "ID"; 
     order.Desc = true; 
     query.OrderByProperty = order; 
     query.Add(new Criterion("CategoryID", categoryID, CriteriaOperator.Equal)); 

     return categoryContentRepository.FindBy(query); 
    } 

categoryContentRepository是CategoryContentRepository類與DB管理工作信息庫項目的對象。

在MVC控制器:

public ActionResult Content(int id) 
    { 
     var contentService = ServiceFactory.CreatContentService(); 
     var response = contentService.GetContents(id).ToList(); 

     return PartialView("_Content", response); 
    } 

鑑於(_content.cshtml):

@model List<Plus.Model.CategoryContent> 

@foreach (var item in Model) 
{ 
    @Html.ActionLink(item.Content.Title, "Amir") 
} 

模型類:

public class Category : ValidationBase, IAggregateRoot 
{ 
    public int ID { get; set; } 

    [NotNullOrEmpty()] 
    public string Name { get; set; } 

    [NotNullOrEmpty()] 
    public int Ordering { get; set; } 

    public int? ParentID { get; set; } 
public Guid ModifiedBy { get; set; } 
public DateTime ModifiedOn { get; set; } 
public string ModifiedReason { get; set; } 
public bool Deleted { get; set; } 
public bool Active { get; set; } 


    public virtual Category Parent { get; set; } 
    public virtual IList<Category> Childeren { get; set; } 
    public virtual IList<CategoryContent> CategoryContents { get; set; } 
} 

public class CategoryContent : ModelBase, IAggregateRoot 
{ 
    public int ID { get; set; } 
    public int ContentID { get; set; } 
    public int CategoryID { get; set; } 

    public virtual Content Content { get; set; } 
    public virtual Category Category { get; set; } 
} 

public class Content : ModelBase, IAggregateRoot 
{ 
    public int ID { get; set; } 
    public string Title { get; set; } 
    public string Intro { get; set; } 
    public int? ParentID { get; set; } 
    public bool IsSeries { get; set; } 
    public byte LikeVoteStatus { get; set; } 

    public virtual IList<SubContent> SubContents { get; set; } 
    public virtual Content Parent { get; set; } 
    public virtual IList<Content> Childeren { get; set; } 
    public virtual CategoryContent CategoryContents { get; set; } 
} 
+1

我們可以看到代碼如何分類,CategoryContent和內容連接? –

+0

感謝評論,我在代碼中添加了代碼。 –

回答

2

內容模型的代碼有缺陷:

public virtual CategoryContent CategoryContents { get; set; } 

和正確的代碼是:

public virtual IList<CategoryContent> CategoryContents { get; set; }