gpt4 book ai didi

c# - 自引用循环 Entity Framework

转载 作者:太空宇宙 更新时间:2023-11-03 23:02:34 25 4
gpt4 key购买 nike

我目前正在使用 WebApi 2.0 和 EntityFrameWork,但遇到了问题:

Self referencing loop detected for property 'UserInfo' with type 'System.Data.Entity.DynamicProxies.UserInfo_F7C6DF3909A804C5A9AC107297C8851F4CC9DF1CCA4A689B892B6C6EBA5A6EA8'. Path '[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 请求,我会得到一个异常: Image of the error

Self referencing loop detected for property '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
}
]

现在我明白了 Entity Framework 中的自引用循环是什么意思;我可以在我的 Get 响应中做什么,只返回不在用户类中的其他值,如用户名、电话、地址、国家等。

最佳答案

try this on startup .cs 

config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
= Newtonsoft.Json.ReferenceLoopHandling.Ignore;

关于c# - 自引用循环 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42562795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com