首页 文章

在现有的多对多关系中添加其他列 - EF Code First

提问于
浏览
4

我正在使用Entity Framework 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; }
}

并且Configuration类正确设置表“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类而不指定新列,但最终还是丢弃了表 .

1 回答

  • 2

    我不认为您可以使用流畅的API做任何事情,它将告诉实体框架您希望保留现有表,从而生成所需的迁移 . 但您可以自己更改迁移中的代码 . 只需删除所有生成的Drops和Creates .

    您发布的迁移代码似乎与您的模型不匹配 . 我希望在create table语句中看到 RuoloDocenteId 列 .

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

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

相关问题