我正在构建一个WebAPI,我已经用5个不同的组织填充了我的数据库,每个组织有3个不同的联系人,每个联系人有3个电话号码和EntityFramework。 但不知何故,它表现得很奇怪:
我的问题:
1-模型之间的关系是否构建错误,因此我只得到一个子元素而不是整个列表?
2-为什么我的路线不接受参数{id}?
这些是我的模特:
**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 :(得分: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);
}
你的模特对我来说很好看。你能展示一下你的存储库吗?