Add an Extra Column to Existing Many, Many Relationships - EF Code First

I am using Entity Framework 6.1.1. I need to add an extra field to the connection table that I created correctly with the Fluent API. I know this is not possible with the Fluent API and that I need to create the model class, but the problem is that I cannot display the current api configuration in the entity model class without ending up dropping the jump table by 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 config class that sets the "LezioneDocente" table correctly:

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 a need to add an additional column to this table (RuoloId), so I need to map the current API configuration to the model class. I am trying to do this by adding a new model class and changing the Docente and Lezione class to reference the new model "LezioniDocente", for example:

    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 the DropTable on the 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 the current API link to the model class without dropping the existing table? I also tried to write the LezioneDocente class without specifying a new column, but it drops the table anyway.

+3


source to share


1 answer


I don't think you can do anything with a fluent API that will tell the entity framework that you want to keep the existing table and thus generate the required migration. But you can change the code yourself during the migration. Just delete all created blobs and creations.

The migration code you posted does not match your model. I would expect to see a column RuoloDocenteId

in the create table statement.



Anyway, 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));

      

+1


source







All Articles