ASP.NET Core+EF Core通讯录:创建联系人时邮箱/手机号未入库
通讯录Web应用关联数据保存问题
开发基于ASP.NET Core(EF Core)的通讯录Web应用,定义了Contact、Email、Phone三个模型类,三者为一对多关联(每个联系人可拥有多个邮箱和手机号)。但通过Swagger UI创建新联系人时,仅联系人基础信息被保存至数据库,关联的邮箱和手机号未存入;手动输入邮箱和手机号提交后,返回的联系人数据中邮箱和手机号数组仍为空。
已查阅官方文档但帮助有限,尝试过的方案未解决问题,不确定是否是JSON输入导致的关联数据未被正确处理。
ContactController 代码
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using incubis_assignment.Data; using incubis_assignment.Models; using System.Linq.Expressions; using System.Diagnostics.Eventing.Reader; namespace incubis_assignment.Controllers { [Route("api/[controller]")] public class ContactController : ControllerBase { private readonly AppDbContext _context; public ContactController(AppDbContext context) { _context = context; } [HttpGet] public async Task<ActionResult<IEnumerable<Contact>>> GetContacts() { return await _context.Contacts.ToListAsync(); } [HttpGet("{id}")] public async Task<ActionResult<Contact>> GetOneContact(int id) { var contact = await _context.Contacts.FindAsync(id); if (contact == null) return NotFound(); return contact; } [HttpPost] public async Task<ActionResult<Contact>> PostContact(Contact contact) { contact.Id = 0; _context.Contacts.Add(contact); await _context.SaveChangesAsync(); var resourceUrl = Url.Action(nameof(GetContacts), new { id = contact.Id }); return Created(resourceUrl, contact); } [HttpPut("{id}")] public async Task<IActionResult> PutContact(int id, [FromBody] Contact contact) { if (id != contact.Id) return BadRequest(); _context.Entry(contact).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ContactExists(id)) return NotFound(); else throw; } return NoContent(); } [HttpDelete("{id}")] public async Task<ActionResult> DeleteContact(int id) { var contact = await _context.Contacts.FindAsync(id); if (contact == null) return NotFound(); _context.Contacts.Remove(contact); await _context.SaveChangesAsync(); return NoContent(); } private bool ContactExists(int id) { return _context.Contacts.Any(e => e.Id == id); } } }
AppDbContext 代码
using Microsoft.EntityFrameworkCore; using incubis_assignment.Models; using System.Reflection.Metadata; namespace incubis_assignment.Data; public class AppDbContext : DbContext { public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } public DbSet<Contact> Contacts { get; set; } public DbSet<Email> Emails { get; set; } public DbSet<Phone> Phones { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Contact>() .HasMany(c => c.Emails) .WithOne(e => e.Contact) .HasForeignKey(e => e.ContactId) .IsRequired(); modelBuilder.Entity<Contact>() .HasMany(c => c.Phones) .WithOne(p => p.Contact) .HasForeignKey(p => p.ContactId) .IsRequired(); } }
Contact 模型类
namespace incubis_assignment.Models { public class Contact { public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } public string Address { get; set; } public ICollection<Email> Emails { get; set; } = new List<Email>(); public ICollection<Phone> Phones { get; set; } = new List<Phone>(); } }
Email 模型类
namespace incubis_assignment.Models { public class Email { public int Id { get; set; } public int ContactId { get; set; } public string EmailAddress { get; set; } public string Category { get; set; } public Contact Contact { get; set; } // Navigacijsko svojstvo, nema potrebe za null! } }
Phone 模型类
namespace incubis_assignment.Models { public class Phone { public int Id { get; set; } public int ContactId { get; set; } public string PhoneNumber { get; set; } public string Category { get; set; } public Contact Contact { get; set; } // Navigacijsko svojstvo, nema potrebe za null! } }
内容的提问来源于stack exchange,提问作者Frane Krapić
相关产品推荐
相关产品推荐

