Entity Framework 6.1.1 を使用しています。Fluent API で正しく作成したジャンクション テーブルに追加のフィールドを追加する必要があります。これは Fluent API では不可能であり、モデル クラスを作成する必要があることはわかっていますが、問題は、ジャンクション テーブルを削除して再作成することなく、流暢な API 構成をエンティティ モデル クラスにマップすることができないことです。それを作成します。
Docente と Lezione の 2 つのモデル クラスがあります。
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 クラスを記述しようとしましたが、とにかくテーブルを削除してしまいます。