1

我有一家餐桌餐厅。一家餐厅可以有多种特色菜。特色菜是中国菜、西班牙菜、希腊菜等。

餐厅餐桌/班级:

[Table("Restaurant")]
public class Restaurant
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required]
    [MaxLength(40)]
    [Column(Order = 2)]
    public string Name { get; set; } 

    [MaxLength(1000)]
    [Column(Order = 6)]
    public string Comments { get; set; }

    public virtual ICollection<Specialties> Specialties { get; set; }
}

这是我的专长表/课程:

 public enum Specialty
    {
        Chinees = 0,
        Spanish = 1,
        Etc.. = 2,
    }

    [Table("Specialties")]
    public class Specialties
    {
        [Key]
        [Column(Order = 0)]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Required]
        [Column(Order = 1)]
        public Specialty Specialty { get; set; } 

        //[Required]
        [MaxLength(20)]
        [Column(Order = 2)]
        public string Name { get; set; } 

        public virtual ICollection<Restaurant> Restaurant { get; set; }
    }

专业表已预先填充了数据。

这是我的多对多关系的表/类:

[Table("RestaurantSpecialties")]
    public class RestaurantSpecialties
    {
        [Key]
        public int RestaurantId { get; set; }

        [Key]
        public int SpecialtyId { get; set; }

        [ForeignKey("RestaurantId")]
        public virtual Restaurant Restaurant{ get; set; }

        [ForeignKey("SpecialtyId")]
        public virtual Specialties Specialty { get; set; }
    }

Fluent Api试图像这样定义模型:

// Primary keys
modelBuilder.Entity<Restaurant>().HasKey(q => q.Id);
modelBuilder.Entity<Specialties>().HasKey(q => q.Id);

// Relationship
modelBuilder.Entity<Restaurant>()
    .HasMany(q => q.Specialties)
    .WithMany(q => q.Restaurant)
    .Map(q =>
    {
        q.ToTable("RestaurantSpecialty");
        q.MapLeftKey("RestaurantId");
        q.MapRightKey("SpecialtyId");
    });

在我的 Seed 函数中,我正在播种这样的数据:

context.Specialties.AddOrUpdate(
    p => p.Specialty,
    new Specialties { Specialty = Specialty.Chinees, Name = "Chinees" },
    new Specialties { Specialty = Specialty.Spanish, Name = "Spanish" },
    );

ICollection<Specialties> lstSpecialties = new List<Specialties>();
lstSpecialties.Add(context.Specialties.FirstOrDefault(x => x.Name == "Chinees"));

context.Restaurants.AddOrUpdate(
  p => p.Name,
  new Restaurant{ Name = "Ctr test1", Specialties = lstSpecialties, Comments = "sfasd" },
  new Restaurant{ Name = "Ctr test2", Specialties = lstSpecialties, Comments = "asd" },
  );

但我的表 RestaurantSpecialties 不包含任何内容。我期待看到两张桌子的身份证。

我究竟做错了什么?

[编辑]

枚举已从“Specialty”重命名为“RestaurantSpecialty”,就像 Gert 建议的那样(只是重命名部分,而不是实际名称)。

复数类别“Specialties”也已重命名为“Specialty”。也因为格特建议。

连接表也已从模型中删除。我原始帖子中的Fluent Api部分已相应更新。

Catering(s) 已重命名为 Restaurant(s),我一开始忘记这样做了。所以为了避免混淆,我在我的原始代码帖子中也这样做了。

context.Specialties.AddOrUpdate(
   new Specialty { CateringSpecialty = RestaurantSpecialty.Chinese, Name = "Chinese" },
   new Specialty { CateringSpecialty = CateringSpecialty.Greeks, Name = "Greeks" },
);


var aSpecialty = context.Specialties.FirstOrDefault(x => x.Name == "Chinese");
var restaurant1 = context.Restaurants.Include(c => c.Specialties).FirstOrDefault(x => x.Name == "Ctr test1");

 if (restaurant1 == null)
 {
    restaurant1 = new Restaurant
    {
         Name = "Ctr test4",
         Specialties = new List<Specialty> { aSpecialty },
         Comments = "testtttttttt"
    };
    context.Restaurants.Add(restaurant1);
 }
 else
 {
   if (!restaurant1.Specialties.Any(s => s.Id == aSpecialty.Id))
        restaurant1.Specialties.Add(aSpecialty);

       restaurant1.Name = "Ctr test4";
       restaurant1.Comments = "testtttt";
 }
4

1 回答 1

1

尝试手动添加/更新。我怀疑该AddOrUpdate方法不支持更新相关实体的完整对象图。如果父实体尚不存在,它可能支持将相关实体与其父实体一起添加。但是如果“Chinees”专业已经在没有相关实体的数据库中,则关系可能不会得到更新。

“手动更新”如下所示:

context.Specialties.AddOrUpdate(
    p => p.Specialty,
    new Specialties { Specialty = Specialty.Chinees, Name = "Chinees" },
    new Specialties { Specialty = Specialty.Spanish, Name = "Spanish" },
    );

var chineesSpecialty = context.Specialties
    .FirstOrDefault(x => x.Name == "Chinees");

var catering1 = context.Caterings.Include(c => c.Specialties)
    .FirstOrDefault(x => x.Name == "Ctr test1");

if (catering1 == null)
{
    catering1 = new Catering
    {
        Name = "Ctr test1",
        Specialties = new List<Specialties> { chineesSpecialty },
        Comments = "sfasd"
    };
    context.Caterings.Add(catering1);
}
else
{
    if (!catering1.Specialties.Any(s => s.Id == chineesSpecialty.Id))
        catering1.Specialties.Add(chineesSpecialty);
    catering1.Comments = "sfasd";
}

// and the same for "Ctr test2"

这个答案中也提到了这一点,但它是在代码优先迁移的早期预览版中,所以我不确定这个限制是否仍然存在。但这可以解释为什么您没有在关系表中获得条目。

您的映射本身对我来说看起来是正确的。(但正如 Gert Arnold 在评论中已经推荐的那样,我真的会删除RestaurantSpecialties看起来多余且只会使您的模型复杂化的实体。)

于 2013-06-18T22:00:32.100 回答