I have the table structure (simplified):
public class Record
{
public Guid? Id { get; set; }
public virtual IList<StatusInstance> StatusInstances { get; set; } = default!;
public Guid? CurrentStatusId { get; set; } = default!;
public virtual StatusInstance CurrentStatus { get; set; } = default!;
}
public class StatusInstance
{
public Guid? RecordId { get; set; }
}
I would like to add to both list and single instance. I have similar tables that i would also like to work this way but obviously this results in a circular dependency warning.
What i have tried and works:
- I can add the record with currentstatus but don’t add to list or add recordId. I can then add subsequent new status after the record is created but the first status is lost.
- I can add the record without any status in either list or current, then add to both after the initial save, however due to the unique way app works this is not possible.
- I could remove the current status and store everything in list, however due to the way i am using entity framework i need a property with a one to one relationship. I can setup in record to select first status as a property however I need to get full list to do this then select first. Don’t want to retrieve full list to do this.
As it seems possible to get around circular dependency issues by saving in a particular order, I’m sure there must be a way to do it on first save. All though i can find aways around it, it would be very convenient to this relationship throughout the system.
For reference i have this code in OnModelCreating
modelBuilder.Entity<Record>()
.HasOne(r => r.CurrentStatus)
.WithOne()
.HasForeignKey<Record>(r => r.CurrentStatusId);
modelBuilder.Entity<Record>()
.HasMany(r => r.StatusInstances)
.WithOne()
.HasForeignKey(r => r.RecordId);
5