I am trying to set multiple relationships between two entities like below:
One company has multiple adresses.
One company has one default address.
One company has one default billing address.
One company has one default delivery address.
public abstract class BaseAddress : AbstractValidatableEntity
{
public AddressType Type { get; set; }
public AddressStatus Status { get; set; }
public Country Country { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Address4 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}
public class CompanyAddress : BaseAddress
{
public Guid CompanyId { get; set; }
public Company Company { get; set; }
}
public class Company : AbstractValidatableEntity
{
public string Name { get; set; }
public List<User> Users { get; set; }
public Guid OwnerId { get; set; }
public User Owner { get; set; }
public List<CompanyAddress> Addresses { get; set; }
public CompanyAddress DefaultAddress { get; set; }
public CompanyAddress DefaultBillingAddress { get; set; }
public CompanyAddress DefaultDeliveryAddress { get; set; }
}
public class CompanyConfiguration : AbstractEntityConfiguration<Company>
{
public override void Configure(EntityTypeBuilder<Company> builder)
{
base.Configure(builder);
// Table name
builder
.ToTable("Companies");
// Columns
builder
.Property(s => s.Name)
.IsRequired(true)
.HasMaxLength(255);
// Relationships
builder
.HasMany(s => s.Users)
.WithOne(u => u.Company)
.HasForeignKey(u => u.CompanyId)
.IsRequired(false)
.HasConstraintName("FK_COMPANY_USERS");
builder
.HasMany(s => s.Addresses)
.WithOne(a => a.Company)
.HasForeignKey(a => a.CompanyId)
.IsRequired()
.HasConstraintName("FK_COMPANY_ADDRESSES")
.OnDelete(DeleteBehavior.Cascade);
builder
.HasOne(s => s.DefaultAddress)
.WithOne()
.HasForeignKey<CompanyAddress>(da => da.CompanyId)
.IsRequired(false)
.HasConstraintName("FK_COMPANY_DEFAULT_ADDRESS")
.OnDelete(DeleteBehavior.Restrict);
builder
.HasOne(s => s.DefaultBillingAddress)
.WithOne()
.HasForeignKey<CompanyAddress>(da => da.CompanyId)
.IsRequired(false)
.HasConstraintName("FK_COMPANY_DEFAULT_BILLING_ADDRESS")
.OnDelete(DeleteBehavior.Restrict);
builder
.HasOne(s => s.DefaultDeliveryAddress)
.WithOne()
.HasForeignKey<CompanyAddress>(da => da.CompanyId)
.HasConstraintName("FK_COMPANY_DEFAULT_DELIVERY_ADDRESS");
// Indexes
builder
.HasIndex(s => s.Name)
.HasDatabaseName("IX_COMPANY_NAME");
}
}
But when creating the database I get the following error:
System.InvalidOperationException: 'Unable to determine the relationship represented by navigation 'Company.Addresses' of type 'List'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'
Note that I am trying to achieve 1 Many-to-One relationship and 3 One-To-One relationships to the same table, and this is what differs from all other questions I have searched here.
I have read multiple similar problems but they all had the following scenario:
One company has multiple adresses.
One company has one default address.
If I remove the second and third One-To-One relationships in my code it works perfectly. I am not sure what to do code-wise.
I do know that I could do this DB-wise. Having one CompanyAdresses table and having these properties on Company table: DefaultAddressId, DefaultBillingAddressId, DefaultDeliveryAddressId.
Thanks in advance to all of you.