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.