实体框架将一对多映射到一对一

Entity Framework map one to many to one

提问人:This lowly newb 提问时间:11/30/2019 最后编辑:marc_sThis lowly newb 更新时间:11/30/2019 访问量:65

问:

我在使用实体框架映射表时遇到了麻烦,其关系是一对多的。

为了清楚起见,下面是表类的图示

public class Human
{
    [Key]
    public long ID { get; set; }

    [Required(ErrorMessage = "must not be empty!", AllowEmptyStrings = false)]
    public string HumanName { get; set; }

    public virtual ICollection<HumanFootwear> HumanFootwears { get; set; }
}

人类鞋类(用于分组)

    public class HumanFootwear
    {
        [Key]
        public long ID { get; set; }

        [ForeignKey("HumanID"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public virtual Human CHumans { get; set; }
        public long HumanID { get; set; }

        [ForeignKey("FootwearID"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public virtual Footwear Footwears { get; set; }
        public long FootwearID { get; set; }
    }

鞋类餐桌类

public class Footwear
{
    [Key]
    public long ID { get; set; }

    [Required(ErrorMessage = "must not be empty!", AllowEmptyStrings = false)]
    public string BrandName { get; set; }
}

我正在尝试使用流畅的 API 方法,但我无法让它工作

        modelBuilder.Entity<Human>()
            .HasKey(key => key.ID)
            .HasMany(hm => hm.Footwear)
            .WithMany(wm => wm.Human)    
            .Map(map =>
            {
                map.MapLeftKey("")
            });

我在“with many human”上遇到语法错误,

无法将单个类转换为HumanICollection<Human>

我做错了吗?我应该绘制人类鞋类的地图吗?

C# 实体框架

评论


答: 暂无答案