Newtonsoft.Json.JsonConvert 没有正确反序列化 EntityCollection

Newtonsoft.Json.JsonConvert not Deserializing EntityCollection Properly

我有一个 MVC Web 应用程序和一个 Web API 服务,我正在尝试从中检索信息。问题是 CapitalMailOrders 实体集合在 Web 端反序列化时缺少项目。

服务使用以下方法检索信息

 var result = db.Contacts
        .Include(a => a.IDXPageLinks)
        .Include(b => b.ReboGatewayLoginInfoes)
        .Include(c => c.SocialMedias)
        .Include(d => d.WebSiteInfoes)
        .Include(e => e.ContactImages)
        .Include(f => f.RealtorSetUpProcesses.Select(f1 => f1.CapitalMailOrders)
        .Include(g => g.Contact_CarrierCode_Assignments)
        .FirstOrDefault(c => c.ContactID == id);

此代码很好,returns 下面是服务端代码。下图显示了 3 个 CapitalMailOrders,这是应该有的。

但是当它在 Web 端反序列化时我只得到 2,第三个是 null

这是 Web 端存储库代码

   public Contact Get(int id)
    {
        var responseStream =
            requestMethod.GetResponseStream(
                requestMethod.getRequest("GET", "application/json",
                    string.Format("{0}/api/contact/{1}", restService, id)).GetResponse());

        var contacts = deSerialize<Contact>(responseStream) as Contact;
        return contacts;
    }

deSerialize 在基础存储库中 class

public class BaseRepository
{
    protected readonly string restService = ConfigurationManager.AppSettings["restService"];

    protected readonly RequestMethod requestMethod = new RequestMethod();
    protected ISerialization _serializer;

    protected BaseRepository()
    { }

    protected object deSerialize<T>(Stream stream)
    {
        var retval = _serializer.DeSerialize<T>(stream);
        return retval;
    }

    protected string serialize<T>(T value)
    {
        var retval = _serializer.Serialize<T>(value);
        return retval;
    }
}


public class JsonNetSerialization : ISerialization
{
    public string Serialize<T>(object o)
    {
        return JsonConvert.SerializeObject((T)o);
    }

    public object DeSerialize<T>(Stream stream)
    {
        return JsonConvert.DeserializeObject<T>(new StreamReader(stream).ReadToEnd());
    }
}

有什么想法吗?谢谢

post pointed me to the problem and suggested solution. In a nutshell the poster @Darin advised to return the data collection from the web api using models rather than the entity collection. Since there is no databinding to the db context when serializing the data back to the web app theres really no reason to carry the overhead and problems of trying to serialize the entity collection. This blog post 更详细。