简体   繁体   中英

Add additional column to existing many to many relationship - EF Code First

I'm using Entity Framework 6.1.1. I need to add an extra field to a junction table, which I correctly created with Fluent API. I know that this is not possible with Fluent API and that I have to create a model class, but the problem is that I cannot manage to map a fluent api configuration to an entity model class without ending up in dropping the junction table and re-creating it.

I have two model classes: Docente and 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; }
}

And the Configuration class which correctly sets up the table "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")
               );
        }

    }

Now I have the necessity of adding an additional column to this table (RuoloId), so I need to map the fluent API configuration to a model class. I try to do this by adding a new model class and by modifying Docente and Lezione class to refer the new model "LezioniDocente", like so:

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


    }
}

When I add a new migration to reflect the changes it inevitably calls a DropTable on LezioneDocente table.

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

Am I missing something? How can I map a fluent API relationship to a model class without dropping the existing table? I also tried to write LezioneDocente class without specifying the new column but it ends up dropping the table anyways.

I don't think you can you can do anything with the fluent API that will tell entity framework that you want to keep the existing table and thus generate the migration you want. But you can change the code in the migration yourself. Just delete all the generated Drops and Creates.

The migration code you have posted does not appear to match your model. I would expect to see a RuoloDocenteId column in the create table statement.

Anyways, this is what I think you want in the Up method instead of the code you posted:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM