Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property name 'ProductId' is already defined

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");
                });
        }
    }
like image 737
Anders Avatar asked Oct 19 '22 02:10

Anders


1 Answers

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.

like image 60
Anders Avatar answered Nov 03 '22 06:11

Anders