简体   繁体   English

序列化错误 ASP.NET Web API

[英]Serialization Error ASP.NET Web API

I have two Model Classes State and City我有两个模型类 State 和 City

public partial class State
{
    public State()
    {
        this.City = new HashSet<City>();
    }

    public int State_Id { get; set; }
    public string State_Name { get; set; }
    public virtual ICollection<City> City { get; set; }
}

public partial class City
{
    public int City_Id { get; set; }
    public string City_Name { get; set; }
    public int State_Id { get; set; }

    public virtual State State { get; set; }
}

My DTO_State Class Is as follows which is for State data transfer objects我的 DTO_State 类如下,用于状态数据传输对象

public class DTO_State
{
    public int State_Id { get; set; }
    public string State_Name { get; set; }
    public ICollection<int> City_Id { get; set; } 
}

In the StateController My Get action method is as follows在StateController中我的Get动作方法如下

This code inside gives me serialization error里面的这段代码给了我序列化错误

    public IQueryable<DTO_State> GetState()
    {   
        var dstates = from S in db.State select new DTO_State()
        {
            State_Id = S.State_Id,
            State_Name=S.State_Name,
            City_Id = S.City.Select(x => x.City_Id).ToList()
       };
        return dstates.AsQueryable();
    }

But If I replace it with this then I get correct data in browser但是如果我用这个替换它,那么我会在浏览器中得到正确的数据

    public IQueryable<DTO_State> GetState()
    {
        List<DTO_State> dstates = new List<DTO_State>();
        IQueryable<State> states = db.State;

        foreach (State S in states)
        {
            DTO_State dstate = new DTO_State();
            dstate.State_Id = S.State_Id;
            dstate.State_Name = S.State_Name;
            dstate.City_Id = S.City.Select(x => x.City_Id).ToList();
            dstates.Add(dstate);
        }
        return dstates.AsQueryable();
    }

Edit: In the following screenshots State,City,DTO_State are same as New_State,New_City and DTO_New_State so please do not get confused编辑:在下面的截图中 State,City,DTO_State 与 New_State,New_City 和 DTO_New_State 相同,所以请不要混淆

Here I will show what happens in debug view for first erroneous code在这里,我将展示第一个错误代码在调试视图中会发生什么

When cursor is over states this happens当光标在状态上时会发生这种情况

When cursor over dstates this happens当光标悬停在 dstates 上时,会发生这种情况

在此处输入图片说明

Now even if multithreading is there dstates should have been evaluated by know but it is not现在,即使存在多线程,dstates 也应该通过 know 进行评估,但事实并非如此

Now for the correct code Screenshots are as follows Here from debugging I observe that as the LINQ Query gets executed on each iteration on pointing cursor over the states variable its count increases by one so whenever the next value of states variable is required it is obtained by some multithreading mechanism (There are total 5 states)现在对于正确的代码屏幕截图如下从调试中我观察到当 LINQ 查询在每次将光标指向状态变量的迭代中执行时,它的计数增加一,因此每当需要状态变量的下一个值时,它是通过一些多线程机制(共有5种状态)

在此处输入图片说明

In the next picture at this point in debugging states and dstates are both populated在下图中此时调试状态和 dstates 都被填充

在此处输入图片说明

Correct Output in Browser在浏览器中正确输出

在此处输入图片说明

So in a nutshell I don't know if LINQ Query is not supporting multithreading or something because WebAPI internally does multithreading so please help me out here所以简而言之,我不知道 LINQ Query 是否不支持多线程或其他原因,因为 WebAPI 内部执行多线程,所以请在这里帮助我

Use .AsEnumerable() or .ToList() instead of .AsQueryable() .使用.AsEnumerable().ToList()而不是.AsQueryable() Or in between your LINQ query and .AsQueryable() if you can't control the interface you're implementing with或者在你的 LINQ 查询和.AsQueryable()如果你不能控制你正在实现的接口

public IQueryable<DTO_State> GetState()

What's the difference(s) between .ToList(), .AsEnumerable(), AsQueryable()? .ToList()、.AsEnumerable()、AsQueryable() 之间有什么区别? will help explain the mechanics.将有助于解释机制。

AsEnumerable is frequently used to switch from any IQueryable implementation to LINQ to objects (L2O) AsEnumerable 经常用于从任何 IQueryable 实现切换到 LINQ to objects (L2O)

Update:更新:

After reviewing the error message, looks like a bug in EF.查看错误消息后,看起来像 EF 中的错误。 Exception on Inner LINQ query when calling ToList() 调用 ToList() 时内部 LINQ 查询异常

You'll have to change ICollection of Cities to IEnumerable.您必须将 ICollection of Cities 更改为 IEnumerable。

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

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