简体   繁体   English

使用MVC 5 API控制器检索具有相关实体的实体时出错

[英]Error when retrieving entities with related entities using MVC 5 API Controller

I have an API Controller which retrieves a list of articles which works well. 我有一个API控制器,可以检索效果很好的文章列表。 I've created a DTO class for my article which is used by my API controller, which includes a list of related objects (Tags). 我已经为我的文章创建了一个DTO类,供API控制器使用,该类包括相关对象(标签)的列表。

The following code works well and retrieves my list of Articles: 以下代码运行良好,并检索了我的文章列表:

public class ArticleDTO
{

    public int ArticleID { get; set; }

    public string Title { get; set; }

    public DateTime DatePublished { get; set; }

    [AllowHtml]
    public string Body { get; set; }

    public  ICollection<Tag> Tags { get; set; }
}

And my API controller (note that I'm not including Tags here in the linq query): 还有我的API控制器(请注意,我在linq查询中未在此处包含标签):

    private IQueryable<ArticleDTO> MapArticles()
    {
        return from p in db.Articles.Include("Tags")
               select new ArticleDTO() {    
                   ArticleID=p.ArticleID,
                   Title = p.Title,
                   Subheading = p.Subheading,
                   DatePublished = p.DatePublished,
                   Body = p.Body,
               };
    }

    public IEnumerable<ArticleDTO> GetArticles()
    {
        return MapArticles().AsEnumerable();
    }

However if I include Tags: 但是,如果我包含标签:

select new ArticleDTO() {    
    ArticleID=p.ArticleID,
    Title = p.Title,
    Subheading = p.Subheading,
    DatePublished = p.DatePublished,
    Body = p.Body,
    Tags = Tags
};

Then I get the following message: 然后我得到以下消息:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; “ ObjectContent`1”类型未能序列化内容类型“ application / xml”的响应主体; charset=utf-8'. 字符集= UTF-8' 。

Type 'System.Data.Entity.DynamicProxies.Tag_5E08D5046E177BA781B97E38A79F1E2BCC3300645A0E505002547644006F116E' with data contract name 'Tag_5E08D5046E177BA781B97E38A79F1E2BCC3300645A0E505002547644006F116E: http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies ' is not expected. :键入“System.Data.Entity.DynamicProxies.Tag_5E08D5046E177BA781B97E38A79F1E2BCC3300645A0E505002547644006F116E”数据合同名称“Tag_5E08D5046E177BA781B97E38A79F1E2BCC3300645A0E505002547644006F116E http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies预计不会”。 Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer. 考虑使用DataContractResolver或将任何静态未知的类型添加到已知类型的列表中-例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型的列表中。

I don't know what this means or how to fix it, so would appreciate any help! 我不知道这意味着什么或如何解决,因此不胜感激!

Thanks... 谢谢...

It's trying to serialize the Tags collection but it's composed by proxies. 它正在尝试序列化Tags集合,但是它是由代理组成的。 What you can do is to also have a TagDTO so you are sure what you are retrieving: 您可以做的是还拥有一个TagDTO,以便您确定要检索的内容:

select new ArticleDTO() {    
    ArticleID=p.ArticleID,
    Title = p.Title,
    Subheading = p.Subheading,
    DatePublished = p.DatePublished,
    Body = p.Body,
    Tags = Tags.Select(t => new TagDTO {
                                         Name = t.Name
                                       })
    };

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

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