自引用循环实体框架 [英] Self referencing loop Entity FrameWork
问题描述
我目前正在使用WebApi 2.0和EntityFrameWork,但遇到了问题:
I'm currently using WebApi 2.0 and EntityFrameWork and I'm having issues with it:
为类型为'System.Data.Entity.DynamicProxies.UserInfo_F7C6DF3909A804C5A9AC107297C8851F4CC9DF1CCA4A689B892B6C6EBA5A6EA8'的属性'UserInfo'检测到自引用循环.路径"[0].用户"."
Self referencing loop detected for property 'UserInfo' with type 'System.Data.Entity.DynamicProxies.UserInfo_F7C6DF3909A804C5A9AC107297C8851F4CC9DF1CCA4A689B892B6C6EBA5A6EA8'. Path '[0].User'."
我的数据库是这样的:User-UserInfo,并且是1-1关系,User的PK是UserInfo中的PF;
my DB is something like this: User-UserInfo and is 1-1 relationship, the PK of User is the PF in UserInfo;
public class User {
public int UserId { set; get; }
public string username { set; get; }
public string password { set; get; }
public string name { set; get; }
public string email { set; get; }
public string surname { set; get; }
public string lastName { set; get; }
public int age { set; get; }
public DateTime regDate { set; get; }
public bool userType { set; get; }
//we define our relationships
//1-1 UserModel-UserInfo
public virtual UserInfo UserInfo { set; get; }
}
我的UserInfo类:
My UserInfo class:
public class UserInfo {
[Key, ForeignKey("User")]
public int UserId { set; get; }
public string username { set; get; }
public string phone{ set; get; }
public string adress { set; get; }
public string country { set; get; }
public string city { set; get; }
public string zip { set; get; }
//we define our relationships
//1-1 UserModel-UserInfo
public virtual User User { set; get; }
}
在邮递员中,我发送一个发布请求并注册一个用户:
In Postman I send a Post request and register a user:
{
"username": "otmanlicona",
"password": "pwd1234",
"name": "Otman",
"email": "otmanlicona17@mail.com",
"surname": "licona",
"lastName": "ledezma",
"age": 33,
"regDate": "2017-03-01T18:10:11+00:00",
"userType": false
}
如果我发送GET请求,则会得到所有用户:
If I send a GET request I get all the users:
[
{
"Orders": [],
"ReviewProducts": [],
"UserInfo": null,
"UserId": 7,
"username": "otmanlicona",
"password": "pwd1234",
"name": "Otman",
"email": "otmanlicona17@mail.com",
"surname": "licona",
"lastName": "ledezma",
"age": 33,
"regDate": "2017-03-01T12:10:11",
"userType": false
},
{
"Orders": [],
"ReviewProducts": [],
"UserInfo": null,
"UserId": 8,
"username": "angelsilva",
"password": "pwd1234",
"name": "angel",
"email": "angelsilva@mail.com",
"surname": "silva",
"lastName": "borja",
"age": 22,
"regDate": "2017-03-01T12:10:11",
"userType": true
}
]
到目前为止没有问题,问题出在我插入UserInfo时:
No problems so far , the problem is when I insert the UserInfo:
{
"UserId": 8,
"username": "angelsilva",
"phone": "12345678",
"adress": "550 Swallow Hill",
"country": "USA",
"city": "foo",
"zip": "47-253"
}
如果我发送另一个GET请求,则会收到异常: 错误图片
If I send another GET request I get an Exception: Image of the error
检测到针对属性'UserInfo'的自引用循环
Self referencing loop detected for property 'UserInfo'
有人可以告诉我我在做什么错吗,谢谢
Can anyone tell me what I'm doing wrong?,Thanks
[
{
"Orders": [],
"ReviewProducts": [],
"UserInfo": null,
"UserId": 7,
"username": "otmanlicona",
"password": "pwd1234",
"name": "Otman",
"email": "otmanlicona17@mail.com",
"surname": "licona",
"lastName": "ledezma",
"age": 33,
"regDate": "2017-03-01T12:10:11",
"userType": false
},
{
"Orders": [],
"ReviewProducts": [],
"UserInfo": {
"UserId": 8,
"username": "angelsilva",
"phone": "12345678",
"adress": "550 Swallow Hill",
"country": "USA",
"city": "foo",
"zip": "47-253"
},
"UserId": 8,
"username": "angelsilva",
"password": "pwd1234",
"name": "angel",
"email": "angelsilva@mail.com",
"surname": "silva",
"lastName": "borja",
"age": 22,
"regDate": "2017-03-01T12:10:11",
"userType": true
}
]
现在,我了解Entity Framework的自引用循环是什么意思;在我的获取"响应中,我该怎么做,只会向我返回用户类中未包含的其他值,例如用户名,电话,地址,国家/地区等.
NOW I see what does Entity Framework meant by self referencing loop; What can I do so in my Get response only returns me the other values that aren't in the User Class like username,phone,address,country, etc.
推荐答案
try this on startup .cs
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
这篇关于自引用循环实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!