I have been on a project with entity framework in DDD pattern. I don’t use the default custom tool for the entity and I have a repository and some model classes.
I have 3 tables in my entity framework:
- Category
- CategoryContent
- Content
Category with categoryContent and content with categoryContent have 1 to many relationship. (I don’t use many to many here because I have some properties in CategoryContent table)
When I send a request for the list of CategoryContent and show content.Title of this list in MVC I have seen this error:
Unable to set field/property CategoryContents on entity type System.Data.Entity.DynamicProxies.Content_731FEFC3E064D6098DC133918117C2359E2F308D2EB1E5D8D986538641AAB7CD.
But when I traced, the program doesn’t have any problems and returned data and show.
I use the message system (request and response message in the service project).
this code is a method for return list of 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 is an object from CategoryContentRepository class in Repository project for management work with DB.
in MVC Controllers:
public ActionResult Content(int id)
{
var contentService = ServiceFactory.CreatContentService();
var response = contentService.GetContents(id).ToList();
return PartialView("_Content", response);
}
in view (_content.cshtml):
@model List<Plus.Model.CategoryContent>
@foreach (var item in Model)
{
@Html.ActionLink(item.Content.Title, "Amir")
}
Model Classes:
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; }
}
Code of content model has a bug:
and right code is: