[英]Self referencing loop in Entity Framework Core models when using InverseProperty
[英]Self referencing loop Entity FrameWork
我目前正在使用WebApi 2.0和EntityFrameWork,我遇到了问题:
检测到属性“UserInfo”的自引用循环,类型为“System.Data.Entity.DynamicProxies.UserInfo_F7C6DF3909A804C5A9AC107297C8851F4CC9DF1CCA4A689B892B6C6EBA5A6EA8”。 路径'[0] .User'。“
我的DB是这样的:User-UserInfo和1-1的关系,User的PK是UserInfo中的PF;
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类:
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; }
}
在Postman我发送一个Post请求并注册一个用户:
{
"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请求,我会收到所有用户:
[
{
"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时:
{
"UserId": 8,
"username": "angelsilva",
"phone": "12345678",
"adress": "550 Swallow Hill",
"country": "USA",
"city": "foo",
"zip": "47-253"
}
如果我发送另一个GET请求,我会得到一个例外: 错误的图像
检测到属性“UserInfo”的自引用循环
谁能告诉我我做错了什么?谢谢
[
{
"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
}
]
现在我看到实体框架的自引用循环意味着什么; 我在Get响应中可以执行的操作仅返回用户类中不包含的其他值,如用户名,电话,地址,国家/地区等。
try this on startup .cs
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.