In my application there is a many-to-many relationship between two entities - Bill and User. Thus a user can have many bills and a bill can apply to many users.
User{
public string Id{get; set;}
public ICollection<Bill> Bills {get; set;}
}
Bill{
public int Id{get; set;}
public ICollection<User> Users {get; set;}
}
Using EF Core 5, I learnt I should be able to ignore the join table of UserBill that contains the IDs of both Entities and a navigational properties to each of the other entities. However, I wish to make sure that no bill occurs twice for a given User.
UserBill{
public string UserId{get; set;}
public User Payer{get; set;}
public int BillId{get; set;}
public Bill Bill {get; set;}
}
So I wish to modify the join table as follows:
Add unique Constraint to the join table but since the join table is automatically generated by EF Core, I am looking for a way to add this unique constraint to the join table
Add other properties to the join table. For instance, I need to add PaymentStatus to the join table to indicate whether the bill has been paid and other properties where necessary
UserBill{ public int Id {get; set;} public string UserId{get; set;} public User Payer{get; set;} public int BillId{get; set;} public Bill Bill {get; set;} public PaymentStatus Status {get; set;} }
Here I now need to add unique constraint to ensure that there cannot be a multiple occurrence of
UserId,BillId
Any assistance to get this done will be appreciated. Thank you.