2016-09-27 18 views
1

我建立一個的WebAPI和我已填充我的數據庫有5個不同的組織,每個組織都有3個不同的聯繫人,每個聯繫人有3個電話號碼用的EntityFramework。 但不知何故,它的行爲以一種不可思議的方式:的EntityFramework關係給出不正確的結果 - 的WebAPI不接受(編號)

  • 我得到聯繫方式和組織
  • 我得到的只是一個組織下的聯繫人的完整列表(GET - > API /組織)
  • 我得到的只是接觸下一個電話(GET - > API /聯繫人)
  • 它不會在API途徑,例如接受參數:API /聯繫人/ {ID}或API /組織{ID}。它返回404代碼。

我的問題:

1-都是模特之間的關係是錯誤的建立,所以我得到的只是一個子元素,而不是整個列表?

2 - 爲什麼我的路線不接受參數(編號)?

這是我的模型:

**Organization:** 
public class Organization { 
     public Guid Id { get; set; } 
     public DateTime dateCreated { get; set; } 
     public string organizationName { get; set; } 
     public virtual ICollection<Contact> Contacts { get; set; } 
    } 

**Contact:** 
public class Contact { 
     public Contact() { } 
     public Guid Id { get; set; } 
     public string firstName { get; set; } 
     public string lastName { get; set; } 
     public virtual ICollection<Phone> Phones { get; set; } 

     // Foreign Key for Organizations 
     public Guid OrganizationId { get; set; } 

     //Related Organization entity 
     [ForeignKey("OrganizationId")] 
     public Organization OrganizationData { get; set; } 
    } 

**Phones:** 
public class Phone { 
     public Guid Id { get; set; } 
     public string customName { get; set; } 
     public string phoneNumber { get; set; } 

     // Foreign Key for Contacts 
     public Guid ContactId { get; set; } 

     //Related Contact entity 
     [ForeignKey("ContactId")] 
     public Contact ContactData { get; set; } 
    } 

這些都是我的控制器:

**OrganizationsController:** 
    [Route("api/organizations")] 
    public class OrganizationsController : Controller 
    { 
      [HttpGet ("")] 
     public IActionResult Get() 
     { 
       var results = _repository.GetAllOrganizations(); 
       return Ok(Mapper.Map<IEnumerable<Organization>>(results)); 
     } 

[HttpGet("")] 
     public IActionResult Get(Guid Id) 
     { 
       var organization = _repository.GetOrganizationById(Id); 
       return Ok(organization); 
     } 
    } 

**ContactsController** 
[Route("api/organization/{id}/contacts")] 
    public class ContactsController : Controller 
    { 

     [HttpGet("")] 
     public IActionResult Get(Guid Id) 
     { 
       var organization = _repository.GetOrganizationById(Id); 
       return Ok(organization.Contacts.ToList()); 
     } 
     } 
    } 

**AllContactsController** 
[Route("api/contacts")] 
     public class AllController : Controller 
     { 
     [HttpGet("")] 
     public IActionResult Get() 
     { 
       var results = _repository.GetAllContacts(); 
       return Ok(Mapper.Map<IEnumerable<Contact>>(results)); 
     } 

     [HttpGet("/{id}")] 
     public IActionResult Get(Guid Id) 
     { 
       var contact = _repository.GetContactById(Id); 
       return Ok(contact); 
     } 

編輯:添加庫:

public class ContactManagementRepository : IContactManagementRepository 
    { 
     private ContactManagementContext _context; 
     private ILogger<ContactManagementRepository> _logger; 

     public ContactManagementRepository(ContactManagementContext context, ILogger<ContactManagementRepository> logger) 
     { 
      _context = context; 
      _logger = logger; 
     } 

     public IEnumerable<Organization> GetAllOrganizations() 
     { 
      _logger.LogInformation("Getting All Organizations from the Database"); 
      return _context.Organizations.ToList(); 
     } 

     public IEnumerable<Contact> GetAllContacts() 
     { 
      _logger.LogInformation("Getting All Contacts from the Database"); 
      return _context.Contacts.ToList(); 
     } 

     public void AddOrganization(Organization organization) 
     { 
      _context.Add(organization); 
     } 

     public void AddContact(Guid id, Contact newContact) 
     { 
      var organization = GetOrganizationById(id); 
      if(organization != null) 
      { 
       organization.Contacts.Add(newContact); 
       _context.Contacts.Add(newContact); 
      } 
     } 

     public async Task<bool> SaveChangesAsync() 
     { 
      return (await _context.SaveChangesAsync()) > 0; 
     } 

     public Organization GetOrganizationById(Guid Id) 
     { 
      return _context.Organizations 
       .Include(c => c.Contacts) 
       .Where(c => c.Id == Id) 
       .FirstOrDefault(); 
     } 

     public Contact GetContactById(Guid Id) 
     { 
      return _context.Contacts 
       .Include(c => c.Addresses) 
       .Include(c => c.Bankdatas) 
       .Include(c => c.Phones) 
       .Where(c => c.Id == Id) 
       .FirstOrDefault(); 
     } 

     public void DeleteContact(Guid id) 
     { 
      var contact = GetContactById(id); 
      if (contact != null) 
      { 
       _context.Contacts.Remove(contact); 
      } 
     } 
    } 
+0

庫看起來很好對我來說太。你能更詳細地說明你的問題嗎?你發送的是什麼,返回的是什麼? – DmitryBLR

+0

我得到的所有聯繫人和所有組織的名單,但是當我試圖從一個組織獲取詳細信息(其3個觸點)我剛剛得到一個接觸。聯繫人也是一樣,我只需要一部電話。 –

回答

0

試用OrganizationsController

[HttpGet("{id}")] 
public IActionResult Get(Guid Id) 
{ 
     var organization = _repository.GetOrganizationById(Id); 
     return Ok(organization); 
} 

,並嘗試AllController

[HttpGet("{id}")] 
public IActionResult Get(Guid Id) 
{ 
     var contact = _repository.GetContactById(Id); 
     return Ok(contact); 
} 

你的模型看起來對我好。你可以顯示你的倉庫嗎?

+0

當然,我添加了存儲庫 –