简体   繁体   English

无法在实体上设置字段/属性

[英]Unable to set field/property on entity

I have been on a project with entity framework in DDD pattern.我一直在使用 DDD 模式的实体框架进行项目。 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:我的实体框架中有 3 个表:

  1. Category类别
  2. CategoryContent分类内容
  3. Content内容

Category with categoryContent and content with categoryContent have 1 to many relationship. Category with categoryContent和 content with categoryContent是一对多的关系。 (I don't use many to many here because I have some properties in CategoryContent table) (我在这里不使用多对多,因为我在 CategoryContent 表中有一些属性)

When I send a request for the list of CategoryContent and show content.Title of this list in MVC I have seen this error:当我发送对CategoryContent列表的请求并在 MVC 中显示此列表的 content.Title 时,我看到了这个错误:

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 .此代码是返回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. categoryContentRepository 是 Repository 项目中 CategoryContentRepository 类的一个对象,用于管理 DB 工作。

in MVC Controllers:在 MVC 控制器中:

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

        return PartialView("_Content", response);
    }

in view (_content.cshtml):在视图(_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:内容模型的代码有一个错误:

public virtual CategoryContent CategoryContents { get; set; }

and right code is:正确的代码是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM