实体框架6弱的实体映射期望在添加迁移时出现密钥

时间:2019-07-15 09:21:12

标签: entity-framework migration entity weak

在我们的域中,我们拥有由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; }

}

然后是ParkingLotGate,它是一个弱实体,不应该具有独立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; }

}

希望在数据库模型中将ParkingLotIdentity,GateNumber和UserId作为此弱实体的PK出现。两者之间的映射为

           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 });

       }

当我想添加迁移时,它会产生如下错误:    EntityType'ParkingLotGate'未定义密钥。为此定义密钥    EntityType。    ParkingLotGates:EntityType:EntitySet'ParkingLotGates'基于类型   没有定义密钥的“ ParkingLotGate”。

我如何使EF理解从属POCO不是实体而是具有复合密钥!? 任何帮助。

0 个答案:

没有答案