Hi @Prathamesh Shende ,
You can set the foreign key to nullable and use InverseProperty
as below:
public class Customer
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
[ForeignKey("AddedBy")]
public int? AddedByID { get; set; }
[ForeignKey("EditedBy")]
public int? EditedByID { get; set; }
[ForeignKey("DeletedBy")]
public int? DeletedByID { get; set; }
public UserLogin AddedBy { get; set; }
public UserLogin EditedBy { get; set; }
public UserLogin DeletedBy { get; set; }
}
public class Vendor
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
[ForeignKey("AddedBy")]
public int? AddedByID { get; set; }
[ForeignKey("EditedBy")]
public int? EditedByID { get; set; }
[ForeignKey("DeletedBy")]
public int? DeletedByID { get; set; }
public UserLogin AddedBy { get; set; }
public UserLogin EditedBy { get; set; }
public UserLogin DeletedBy { get; set; }
}
public class UserLogin
{
public UserLogin()
{
CustomersAdded = new List<Customer>();
VendorsAdded = new List<Vendor>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
[InverseProperty("AddedBy")]
public List<Customer> CustomersAdded { get; set; }
[InverseProperty("EditedBy")]
public List<Customer> CustomersEdited { get; set; }
[InverseProperty("DeletedBy")]
public List<Customer> CustomersDeleted { get; set; }
[InverseProperty("AddedBy")]
public List<Vendor> VendorsAdded { get; set; }
[InverseProperty("EditedBy")]
public List<Vendor> VendorsEdited { get; set; }
[InverseProperty("DeletedBy")]
public List<Vendor> VendorsDeleted { get; set; }
}
And configure the cascading delete as NoAction
:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<UserLogin> UserLogin { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Vendor> Vendors { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Customer>()
.HasOne(c => c.AddedBy)
.WithMany(c=>c.CustomersAdded)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Customer>()
.HasOne(c => c.EditedBy)
.WithMany(c => c.CustomersEdited)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Customer>()
.HasOne(c => c.DeletedBy)
.WithMany(c => c.CustomersDeleted)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Vendor>()
.HasOne(c => c.AddedBy)
.WithMany(c => c.VendorsAdded)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Vendor>()
.HasOne(c => c.EditedBy)
.WithMany(c=>c.VendorsEdited)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<Vendor>()
.HasOne(c => c.DeletedBy)
.WithMany(c=>c.VendorsDeleted)
.OnDelete(DeleteBehavior.NoAction);
}
}
The generated table as below:
Then, you can add/remove the entity via the ApplicationDbContext, like this:
_context.UserLogin.Add(new UserLogin()
{
Name="AA",
CustomersAdded= new List<Customer>()
{
new Customer(){ Name="Tom"}
},
CustomersEdited = new List<Customer>()
{
new Customer(){ Name="John"}
},
VendorsAdded = new List<Vendor>()
{
new Vendor(){ Name="Jack"}
}
});
_context.SaveChanges();
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion