I have an entity that has a relation using the FK ProductId, I then have another relation on the same entity using the composite keys ProductId and VehicleId. This does not work. I get
One or more validation errors were detected during model generation:
ProductId: Name: Each property name in a type must be unique. Property name 'ProductId' is already defined.
Config code
public class BookingConfiguration : EntityTypeConfiguration<Booking>
{
public BookingConfiguration()
{
...
HasRequired(b => b.Product)
.WithMany(p => p.Bookings)
.Map(m =>
{
m.MapKey("ProductId");
});
HasRequired(b => b.Vehicle)
.WithMany(v => v.Bookings)
.Map(m =>
{
m.MapKey("ProductId","VehicleId");
});
}
}
Steve Greene put me on the right track, I added ProductId
and VehicleId
to the Entity and used
HasRequired(b => b.Vehicle)
.WithMany()
.HasForeignKey(b => new {b.ProductId, b.VehicleId});
In a domain perspective I don't like adding the Foreign keys to the entity so if someone has a better solution please let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With