多对多 CRUD 操作 ASP.Net 核心
Many to Many CRUD operations ASP.Net Core
我有三个 tables- 联系人、地址和 ContactAddress(join table)。这是 tables:
using System;
using System.Collections.Generic;
namespace ProjectWayneAPI.Models
{
public partial class Contact
{
public Guid Id { get; set; }
public DateTime? DateEntered { get; set; }
public bool? Deleted { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneHome { get; set; }
public string Email { get; set; }
public virtual ICollection<ContactAddress> ContactAddress { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace ProjectWayneAPI.Models
{
public partial class Address
{
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public string Lot { get; set; }
public string Road { get; set; }
public string Street { get; set; }
public string Suburb { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Postcode { get; set; }
public string Country { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public bool? Deleted { get; set; }
public virtual ICollection<ContactAddress> ContactAddress { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace ProjectWayneAPI.Models
{
public partial class ContactAddress
{
public Guid Id { get; set; }
public DateTime? Created { get; set; }
public bool? Deleted { get; set; }
public Guid? ContactId { get; set; }
public Guid? AddressId { get; set; }
public virtual Address Address { get; set; }
public virtual Contact Contact { get; set; }
}
}
为简单起见,我没有使用存储库模式。
create操作有两种场景:
- 在创建联系人和地址时填充 ContactAddress table table
- 在创建联系人和地址后填充加入 table table
为了我们的业务需求,我正在尝试调整第一个,因为用户将在用户点击 "save" 按钮时同时填充联系人和地址 table。
我该怎么做(在创建两个实体时填充链接实体。),以及如何在更新实体时更新多对多关系?还有删除操作。
如果您的 Guid Id
是在数据库级别生成的(它是在插入后分配的)。对于您的情况:插入 Contact
和 Address
,以及 link,您必须先插入 Contact
和 Address
,如下所示:
var contact = new Contact();
var address = new Address();
dbContext.Add(contact);
dbContext.Add(address);
dbContext.SaveChanges();
var contactAddress = new ContactAddress()
{
AddressId = address.Id,
ContactId = contact.Id
}
dbContext.Add(contactAddress);
dbContext.SaveChanges();
我有三个 tables- 联系人、地址和 ContactAddress(join table)。这是 tables:
using System;
using System.Collections.Generic;
namespace ProjectWayneAPI.Models
{
public partial class Contact
{
public Guid Id { get; set; }
public DateTime? DateEntered { get; set; }
public bool? Deleted { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneHome { get; set; }
public string Email { get; set; }
public virtual ICollection<ContactAddress> ContactAddress { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace ProjectWayneAPI.Models
{
public partial class Address
{
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public string Lot { get; set; }
public string Road { get; set; }
public string Street { get; set; }
public string Suburb { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Postcode { get; set; }
public string Country { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
public bool? Deleted { get; set; }
public virtual ICollection<ContactAddress> ContactAddress { get; set; }
}
}
using System;
using System.Collections.Generic;
namespace ProjectWayneAPI.Models
{
public partial class ContactAddress
{
public Guid Id { get; set; }
public DateTime? Created { get; set; }
public bool? Deleted { get; set; }
public Guid? ContactId { get; set; }
public Guid? AddressId { get; set; }
public virtual Address Address { get; set; }
public virtual Contact Contact { get; set; }
}
}
为简单起见,我没有使用存储库模式。
create操作有两种场景:
- 在创建联系人和地址时填充 ContactAddress table table
- 在创建联系人和地址后填充加入 table table
为了我们的业务需求,我正在尝试调整第一个,因为用户将在用户点击 "save" 按钮时同时填充联系人和地址 table。
我该怎么做(在创建两个实体时填充链接实体。),以及如何在更新实体时更新多对多关系?还有删除操作。
如果您的 Guid Id
是在数据库级别生成的(它是在插入后分配的)。对于您的情况:插入 Contact
和 Address
,以及 link,您必须先插入 Contact
和 Address
,如下所示:
var contact = new Contact();
var address = new Address();
dbContext.Add(contact);
dbContext.Add(address);
dbContext.SaveChanges();
var contactAddress = new ContactAddress()
{
AddressId = address.Id,
ContactId = contact.Id
}
dbContext.Add(contactAddress);
dbContext.SaveChanges();