Reputation: 1
Is there any way to represent something like this :
public abstract class DbEntity
{
[Key]
public string Id { get; set; } // <-- a GUID-String created by the domain
}
public class Carrier : DbEntity
{
public string Name { get; set; } = default!;
public ICollection<Address> Addresses { get; set; }
}
public class Customer : DbEntity
{
public string Name { get; set; } = default!;
public ICollection<Address> Addresses { get; set; }
}
public class Address
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string? Street { get; set; }
}
Entity Framework will always create the address-table like this :
The problem is, that multiple tables will have an list of addresses (not just two) and I really don't want something like :
I've also tried to set up my entities like this :
_modelBuilder?.Entity<Carrier>()
.HasMany(entity => entity.Addresses);
Which will also create the navigation properties on the child.
I'm also understanding why, because its a many-to-one relationship and the child needs to know his parent.
However I really have no ideas to solve my 'problem'.
I was thinking about creating multiple classes/tables for my addresses like 'CustomerAddress' and 'CarrierAddress' (which inheritance from 'address'), but creating a new address table for every parent.. feels wrong?
Is this really the right way or can I solve this somehow else or is something considered 'good practise' for such cases?
Upvotes: 0
Views: 560
Reputation: 108
You can use Address class with a generic type, but this will generate multiple tables with different names like this:
Address<Parent1>
Address<Parent2>
Address<Parent3>
If this is ok with you, you can modify Address class as:
public class Address<TParent>
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string? Street { get; set; }
public TParent Parent { get; set; }
}
Upvotes: 0