ICollection 循环引用
ICollection circular reference
我有一个像这样的 LINQ 查询
var Data = from a in db.Medlem
where a.Person.PersonId.Equals(BrukerCv.Person.PersonId)
select new
{
a.Prosjekt.ProsjektId,
a.Prosjekt.Navn,
a.Prosjekt.Kunde,
a.Prosjekt.TekniskProfil,
a.MedlemId,
a.Rolle,
a.Start,
a.Slutt
};
并且我想 return 它像 json,所以我使用这个
return Json(Data, JsonRequestBehavior.AllowGet);
但是我遇到的问题是报错
A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.CVVersjon_4F0DBDC27A40F2FDF90760ABFDE49CBE0B75F52902B1C5F1E466393543AC44E8'.
我想这是因为 a.Prosjekt.TekniskProfil
这是一个 ICollection
那么我怎样才能得到 ICollection?
例如,我不想要 ICollection TekniskProfil 中的所有值,只想要某些值
在 TekniskProfil
table 中有一个引用回到 Prosjekt
table,我猜这是我不想要的
您可以select仅投影数据时需要的值:
select new
{
ProsjektId = a.Prosjekt.ProsjektId,
Navn = a.Prosjekt.Navn,
Kunde = a.Prosjekt.Kunde,
TekniskProfil = a.Prosjekt.TekniskProfil.Select(x => new
{
// Select only the properties you need here and avoid
// circular references - exclude all properties that contain
// circular object graphs
Foo = x.Foo,
Bar = x.Bar,
}),
...
};
问题的根本原因很可能是 a.Prosjekt.TekniskProfil 包含对象,其中该对象包含对 medlem 的引用。
在序列化过程中,然后检测到 medlem 实际上已经被序列化,因此你得到错误。
Darin 提供的投影解决方案解决了这个问题,但我觉得很高兴知道你为什么也会出现这个错误。
我不会说您的语言,因此很难推断出发生了什么。但是每当我遇到这个错误时,就是在序列化过程中它以某种方式找到了返回到首先被序列化的对象的方式。如果连载从哪里继续,那就死循环了。
我有一个像这样的 LINQ 查询
var Data = from a in db.Medlem
where a.Person.PersonId.Equals(BrukerCv.Person.PersonId)
select new
{
a.Prosjekt.ProsjektId,
a.Prosjekt.Navn,
a.Prosjekt.Kunde,
a.Prosjekt.TekniskProfil,
a.MedlemId,
a.Rolle,
a.Start,
a.Slutt
};
并且我想 return 它像 json,所以我使用这个
return Json(Data, JsonRequestBehavior.AllowGet);
但是我遇到的问题是报错
A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.CVVersjon_4F0DBDC27A40F2FDF90760ABFDE49CBE0B75F52902B1C5F1E466393543AC44E8'.
我想这是因为 a.Prosjekt.TekniskProfil
这是一个 ICollection
那么我怎样才能得到 ICollection?
例如,我不想要 ICollection TekniskProfil 中的所有值,只想要某些值
在 TekniskProfil
table 中有一个引用回到 Prosjekt
table,我猜这是我不想要的
您可以select仅投影数据时需要的值:
select new
{
ProsjektId = a.Prosjekt.ProsjektId,
Navn = a.Prosjekt.Navn,
Kunde = a.Prosjekt.Kunde,
TekniskProfil = a.Prosjekt.TekniskProfil.Select(x => new
{
// Select only the properties you need here and avoid
// circular references - exclude all properties that contain
// circular object graphs
Foo = x.Foo,
Bar = x.Bar,
}),
...
};
问题的根本原因很可能是 a.Prosjekt.TekniskProfil 包含对象,其中该对象包含对 medlem 的引用。 在序列化过程中,然后检测到 medlem 实际上已经被序列化,因此你得到错误。
Darin 提供的投影解决方案解决了这个问题,但我觉得很高兴知道你为什么也会出现这个错误。
我不会说您的语言,因此很难推断出发生了什么。但是每当我遇到这个错误时,就是在序列化过程中它以某种方式找到了返回到首先被序列化的对象的方式。如果连载从哪里继续,那就死循环了。