简体   繁体   中英

Many to Many CRUD operations ASP.Net Core

I have three tables- Contact, Address, and ContactAddress(join table). Here are the 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; }
}
}

For simplicity, I am not using the Repository Pattern.

For create operation, there are two scenarios:

  • Populating ContactAddress table while creating the Contact and Address table
  • Populating the Join table after creating both the Contact and Address table

For our business requirement, I am trying to adapt, the first one because the user will populate the contact and address table at the same time while the user hits "save" button.

How do I do that one(populating the linkup entity while creating both the entities.), also how do i update the many to many relationship while updating the entity? And also the delete operation.

If your Guid Id is generated at the database level (it's assigned after insert). for your case: insert Contact and Address , and link them, you have to first insert Contact and Address like this:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM