自引用循环实体框架

时间:2017-03-02 18:09:14

标签: c# entity-framework json.net

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

  

为属性&User用户信息检测到自引用循环。类型为' System.Data.Entity.DynamicProxies.UserInfo_F7C6DF3909A804C5A9AC107297C8851F4CC9DF1CCA4A689B892B6C6EBA5A6EA8'。路径' [0]。用户'。"

我的数据库是这样的: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

  

检测到属性“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响应中可以执行的操作仅返回用户类中不包含的其他值,如用户名,电话,地址,国家/地区等。

1 个答案:

答案 0 :(得分:3)

try this on startup .cs 

 config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling 
= Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
相关问题