0

in our domain we have entity aggregate which is consist of ParkingLot :

public class ParkingLot : EntityBase
{
    public string Title { get; set; }

    public int EntryNumber { get; set; }

    public int Floor { get; set; }

    public int Capacity { get; set; }

    public int FilledCapacity { get; set; }

    public int? BuildingId { get; set; }

    public int ParkingSlotId { get; set; }

    public virtual ICollection<ParkingLotGate> ParkingLotGates { get; set; }

}

And then ParkingLotGate which is a weak entity that does not deserve to have an independent Id:

public class ParkingLotGate 
  {
    public int ParkingLotIdentity { get; set; }

    public int GateNumber { get; set; }

    public int UserId { get; set; }

    public  virtual ParkingLot ParkingLot { get; set; }

    public virtual User User { get; set; }

}

It is desired that the ParkingLotIdentity , GateNumber , UserId to be present as the PK of this weak entity in the database model.The mapping between the two is as

           public ParkingLotGateMap()
            {
              this.HasRequired(plg => plg.ParkingLot)
              .WithMany(pl => pl.ParkingLotGates)
              .HasForeignKey(plg => plg.ParkingLotIdentity);

            this.HasRequired(plg => plg.User)
            .WithMany(pl => pl.ParkingLotGates)
            .HasForeignKey(plg => plg.UserId);

             this.HasKey(plg => new { 
               plg.UserId,plg.ParkingLotIdentity, 
            plg.GateNumber });

       }

When I want to add migration it produces the error like this : EntityType 'ParkingLotGate' has no key defined. Define the key for this EntityType. ParkingLotGates: EntityType: EntitySet 'ParkingLotGates' is based on type 'ParkingLotGate' that has no keys defined.

How would I EF make understand that the dependent POCO is not an entity and has a composite key!? Any help apperciated.

BehiAlav
  • 178
  • 7
  • 1
    Make sure `ParkingLotGateMap` is executed, e.g. you have `modelBuilder.Configurations.AddFromAssembly(…);`or `modelBuilder.Configurations.Add(new ParkingLotGateMap());` – Ivan Stoev Jul 15 '19 at 09:31
  • Thanks a lot Ivan, the migration has been committed. – BehiAlav Jul 15 '19 at 09:37

0 Answers0