2017-07-07 81 views
1

我需要我的ASP.NET Web Api 2項目幫助,我可以成功發送JSON數據,我想保存Invites和被邀請的成員。ASP.NET Web Api 2代碼第一個JSON數組返回null

帖子已成功保存數據上的LocalDB

我用的Visual Studio 2017年的腳手架,這是我第一次嘗試在.NET環境

{ 
    "InviteID": 6, 
    "Subject": "sddsdsds", 
    "DateTime": "54545:55", 
    "Members": [ 
     { 
      "MemberID": 8, 
      "MemberName": "sdds", 
      "IsAdmin": false, 
      "MemberNumber": 0, 
      "PhoneNumber": null, 
      "InviteID": 6 
     }, 
     { 
      "MemberID": 9, 
      "MemberName": "sdds", 
      "IsAdmin": false, 
      "MemberNumber": 0, 
      "PhoneNumber": null, 
      "InviteID": 6 
     }, 
     { 
      "MemberID": 10, 
      "MemberName": "sdds", 
      "IsAdmin": false, 
      "MemberNumber": 0, 
      "PhoneNumber": null, 
      "InviteID": 6 
     } 
    ] 
} 

但是當我嘗試檢索邀請,成員數組爲null:

[ 
    { 
     "InviteID": 2, 
     "Subject": null, 
     "DateTime": null, 
     "Members": null 
    }, 
    { 
     "InviteID": 3, 
     "Subject": null, 
     "DateTime": null, 
     "Members": null 
    }, 
    { 
     "InviteID": 4, 
     "Subject": "sddsdsds", 
     "DateTime": "54545:55", 
     "Members": null 
    }, 
    { 
     "InviteID": 5, 
     "Subject": "sddsdsds", 
     "DateTime": "54545:55", 
     "Members": null 
    }, 
    { 
     "InviteID": 6, 
     "Subject": "sddsdsds", 
     "DateTime": "54545:55", 
     "Members": null 
    } 
] 

我的邀請模型

public class Invite 
{ 
    public int InviteID { get; set; } 
    public string Subject { get; set; } 
    public string DateTime { get; set; } 
    public virtual List<Member> Members { get; set; } 
} 

我的會員模式

public class Member 
{ 
    public int MemberID { get; set; } 
    public string MemberName { get; set; } 
    public bool IsAdmin { get; set; } 
    public int MemberNumber { get; set; } 
    public string PhoneNumber { get; set; } 
    public int InviteID { get; set; } 
    [JsonIgnore] 
    public virtual Invite Iinvite { get; set; } 
} 

我控制器

public class InvitesController : ApiController 
{ 
    private InviteContext db = new InviteContext(); 

    public InvitesController() 
    { 
     db.Configuration.ProxyCreationEnabled = false; 
    } 

    // GET: api/Invites 
    public IEnumerable<Invite> Getinvites() 
    { 
     return db.invites.ToList(); 
    } 

    // GET: api/Invites/5 
    [ResponseType(typeof(Invite))] 
    public IHttpActionResult GetInvite(int id) 
    { 
     Invite invite = db.invites.Find(id); 

     if (invite == null) 
     { 
      return NotFound(); 
     } 

     return Ok(invite); 
    } 

    // PUT: api/Invites/5 
    [ResponseType(typeof(void))] 
    public IHttpActionResult PutInvite(int id, Invite invite) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     if (id != invite.InviteID) 
     { 
      return BadRequest(); 
     } 

     db.Entry(invite).State = EntityState.Modified; 

     try 
     { 
      db.SaveChanges(); 
     } 
     catch (DbUpdateConcurrencyException) 
     { 
      if (!InviteExists(id)) 
      { 
       return NotFound(); 
      } 
      else 
      { 
       throw; 
      } 
     } 

     return StatusCode(HttpStatusCode.NoContent); 
    } 

    // POST: api/Invites 
    [ResponseType(typeof(Invite))] 
    public IHttpActionResult PostInvite(Invite invite) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     db.invites.Add(invite); 
     db.SaveChanges(); 

     return CreatedAtRoute("DefaultApi", new { id = invite.InviteID }, invite); 
    } 

    // DELETE: api/Invites/5 
    [ResponseType(typeof(Invite))] 
    public IHttpActionResult DeleteInvite(int id) 
    { 
     Invite invite = db.invites.Find(id); 

     if (invite == null) 
     { 
      return NotFound(); 
     } 

     db.invites.Remove(invite); 
     db.SaveChanges(); 

     return Ok(invite); 
    } 

    protected override void Dispose(bool disposing) 
    { 
     if (disposing) 
     { 
      db.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    private bool InviteExists(int id) 
    { 
     return db.invites.Count(e => e.InviteID == id) > 0; 
    } 
} 

我的分貝範圍內

public class InviteContext : DbContext 
{ 
    public DbSet<Invite> invites { get; set; } 
    public DbSet<Member> members { get; set; } 
} 

編輯 **詢問成員表**

[ 
    { 
     "MemberID": 1, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 2 
    }, 
    { 
     "MemberID": 2, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 2 
    }, 
    { 
     "MemberID": 3, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 3 
    }, 
    { 
     "MemberID": 4, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 4 
    }, 
    { 
     "MemberID": 5, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 5 
    }, 
    { 
     "MemberID": 6, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 5 
    }, 
    { 
     "MemberID": 7, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 5 
    }, 
    { 
     "MemberID": 8, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 6 
    }, 
    { 
     "MemberID": 9, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 6 
    }, 
    { 
     "MemberID": 10, 
     "MemberName": "sdds", 
     "IsAdmin": false, 
     "MemberNumber": 0, 
     "PhoneNumber": null, 
     "InviteID": 6 
    } 
] 
+0

在DB是他們從邀請的外鍵成員表? – LiverpoolOwen

+0

是的,Members表中有InviteID外鍵,並且InviteID被插入到Members表中,請檢查我是否單獨顯示了成員的數據。 –

回答

0

您需要將ProxyCreationEnabled設置爲true才能加載外鍵表的數據。

public InvitesController() 
{ 
    db.Configuration.ProxyCreationEnabled = true; 
} 

如果你是選擇ProxyCreationEnabled設置爲false,那麼你需要編寫LINQ查詢來獲取數據

public IEnumerable<Invite> Getinvites() 
{ 
    return db.invites.Include("Members").ToList(); 
} 
+0

這兩個建議都像魅力一樣......謝謝 –

0

問題可能是由於從數據庫延遲加載成員時,收到的數據。爲了測試,如果這就是下面的代碼的情況下(不要在生產中使用!):

// GET: api/Invites 
public IEnumerable<Invite> Getinvites() 
{ 
    var invites = db.invites.ToList() 
     .ForEach(invite => invite.Members = db.members 
        .Where(m => m.InviteId == invite.InviteId) 
        .ToList()) 
    return invites; 
} 

這將迫使成員國收集的渴望負荷爲每個邀請。如果在運行此代碼之後,您將得到預期的結果,您將需要配置數據庫上下文以便熱切加載數據。

請勿使用以上代碼製作!它不僅不是最理想的,但它也給數據庫帶來了很多壓力。