自引用循环实体框架

问题描述 投票:0回答:2

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

检测到类型为“System.Data.Entity.DynamicProxies.UserInfo_F7C6DF3909A804C5A9AC107297C8851F4CC9DF1CCA4A689B892B6C6EBA5A6EA8”的属性“UserInfo”的自引用循环。路径“[0].用户”。”

我的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": "[email protected]",
  "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": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "surname": "silva",
    "lastName": "borja",
    "age": 22,
    "regDate": "2017-03-01T12:10:11",
    "userType": true
  }
]

现在我明白实体框架中的自引用循环是什么意思了;我可以在“获取”响应中做什么,只返回用户类中不存在的其他值,例如用户名、电话、地址、国家/地区等。

c# entity-framework json.net
2个回答
3
投票
try this on startup .cs 

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

0
投票

试试这个: JsonSerializerSettings 设置 = new JsonSerializerSettings(); settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
var contentToWriteToFile = JsonConvert.SerializeObject(数据,设置);

© www.soinside.com 2019 - 2024. All rights reserved.