Property name 'ProductId' is already defined

c# entity-framework entity-framework-6

Question

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");
                });
        }
    }
1
5
9/11/2015 12:13:33 PM

Accepted Answer

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.

3
9/14/2015 9:15:43 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow