向现有的多对多关系添加其他列 - EF 代码优先

本文关键字:EF 代码 其他 添加 关系 | 更新日期: 2023-09-27 18:34:22

我正在使用实体框架 6.1.1。我需要向连接表添加一个额外的字段,这是我使用 Fluent API 正确创建的。我知道这在 Fluent API 中是不可能的,并且我必须创建一个模型类,但问题是我无法设法将流畅的 api 配置映射到实体模型类,而不会最终删除联结表并重新创建它。

我有两个模型班:Docente和Lezione。

public class Docente
{
    public int Id { get; set; }
    public string Nome { get; set; }
    [Required]
    public string Cognome { get; set; }
    public virtual ICollection<Lezione> LezioniDocente { get; set; }
}
public class Lezione
{
    public int Id { get; set; }
    public string Programma { get; set; }
    public virtual ICollection<Docente> LezioniDocente { get; set; }
}

以及正确设置表"LezioneDocente"的配置类:

public class LezioneConfiguration: EntityTypeConfiguration<Lezione>
    {
        public LezioneConfiguration()
        {
            ToTable("Lezioni");
            Property(c => c.TS).IsRowVersion();
            HasMany(d => d.LezioniDocente).WithMany(e => e.LezioniDocente).Map(
           w => w.ToTable("LezioneDocente")
               .MapLeftKey("LezioneId")
              .MapRightKey("DocenteId")
               );
        }
    }

现在我有必要向此表添加一列 (RuoloId(,因此我需要将流畅的 API 配置映射到模型类。我尝试通过添加新的模型类并修改 Docente 和 Lezione 类来引用新模型"LezioniDocente"来做到这一点,如下所示:

    public class LezioneDocente
    {
        [Key, Column(Order = 0)]
        public int LezioneId { get; set; }
        [Key, Column(Order = 1)]
        public int DocenteId { get; set; }
        public int RuoloDocenteId { get; set; }
        public virtual Docente Docente { get; set; }
        public virtual Lezione Lezione { get; set; }
        public virtual RuoloDocente RuoloDocente { get; set; }
  }
public class Docente
    {
        public int Id { get; set; }
        public string Nome { get; set; }
        [Required]
        public string Cognome { get; set; }
        public virtual ICollection<LezioneDocente> LezioniDocente { get; set; }
    }
public class Lezione
{
        public int Id { get; set; }
        public string Programma { get; set; }
        public virtual ICollection<LezioneDocente> LezioniDocente { get; set; }
}
 public class LezioneDocenteConfiguration : EntityTypeConfiguration<LezioneDocente>
{
    public LezioneDocenteConfiguration()
    {
        HasRequired(_ => _.Lezione)
      .WithMany(_ => _.LezioniDocente)
      .HasForeignKey(_ => _.LezioneId);
        HasRequired(_ => _.Docente)
            .WithMany(_ => _.LezioniDocente)
            .HasForeignKey(_ => _.DocenteId);
        ToTable("LezioneDocente");

    }
}

当我添加新的迁移以反映更改时,它不可避免地称为LezioneDocente表上的DropTable。

public partial class test : DbMigration
    {
        public override void Up()
        {
            DropForeignKey("dbo.LezioneDocente", "LezioneId", "dbo.Lezioni");
            DropForeignKey("dbo.LezioneDocente", "DocenteId", "dbo.Docenti");
            DropIndex("dbo.LezioneDocente", new[] { "LezioneId" });
            DropIndex("dbo.LezioneDocente", new[] { "DocenteId" });
            CreateTable(
                "dbo.LezioneDocente",
                c => new
                    {
                        LezioneId = c.Int(nullable: false),
                        DocenteId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.LezioneId, t.DocenteId })
                .ForeignKey("dbo.Docenti", t => t.DocenteId, cascadeDelete: true)
                .ForeignKey("dbo.Lezioni", t => t.LezioneId, cascadeDelete: true)
                .Index(t => t.DocenteId)
                .Index(t => t.LezioneId);
            DropTable("dbo.LezioneDocente");
        }
}

我错过了什么吗?如何在不删除现有表的情况下将流畅的 API 关系映射到模型类?我还尝试编写 LezioneDocente 类而不指定新列,但它最终还是删除了表。

向现有的多对多关系添加其他列 - EF 代码优先

我认为你不能用流畅的 API 做任何事情来告诉实体框架你想要保留现有表,从而生成你想要的迁移。但您可以自己更改迁移中的代码。只需删除所有生成的拖放和创建即可。

您发布的迁移代码似乎与您的型号不匹配。我希望在创建表语句中看到一个RuoloDocenteId列。

无论如何,这就是我认为您想要的 Up 方法而不是您发布的代码:

AddColumn("dbo.LezioneDocente", "RuoloDocenteId", c => c.Int(nullable: false));