0

これらは私のテーブルです:CFFPart、Disp、CFFPartDisp、Expert、CFFpartDispExpert、CFFPartDispには、CFFPartとDispの間に多くの関係があります。

ToTable("Discipline", "dbr");
        Property(t => t.Id).HasColumnName("DispID");
        Property(t => t.Text).HasColumnName("DisPName");

        HasMany(t => t.CFFParts).WithMany().Map(m => m.MapLeftKey("DisPID").MapRightKey("CFFPartID").ToTable("CFFPartDisP", "dbr"));

CFFpartDisPExpertには、ExpertとCFFpartDisPの間に多くの関係があります。最初にコードでこのマッピングを作成するにはどうすればよいですか?

4

1 に答える 1

1

CFFPartDispモデル内のエンティティクラスとして公開する必要があります。CFFPart質問のFluentマッピング間およびFluentマッピングとのリンクテーブルとして使用することはできませんDisp。との間の関係は、多対多の関係CFFPartではありません(厳密なEFの意味で)。代わりに、中間エンティティとしてDisp2つの1対多の関係を作成する必要があります。次に、この中間エンティティCFFPartDisp間の関係を3番目の関係としてリンクできます。CFFPartDispExpert

エンティティは次のCFFPartDispようになります。

public class CFFPartDisp
{
    public int ID { get; set; }

    public int CFFPartID { get; set; }
    public CFFPart CFFPart { get; set; }

    public int DispID { get; set; }
    public Disp Disp { get; set; }

    public ICollection<Expert> Experts { get; set; }
}

CFFPartおよびエンティティには、以下Dispを参照するコレクションが必要ですCFFPartDisp

public class CFFPart
{
    public int ID { get; set; }

    public ICollection<CFFPartDisp> CFFPartDisps { get; set; }
}

public class Disp
{
    public int ID { get; set; }

    public ICollection<CFFPartDisp> CFFPartDisps { get; set; }
}

また、との間の多対多の関係を確立するためのExpertコレクションも必要です。CFFPartDispCFFPartDispExpert

public class Expert
{
    public int ID { get; set; }

    public ICollection<CFFPartDisp> CFFPartDisps { get; set; }
}

これらのエンティティを使用して、次の3つの関係を作成できます。

modelBuilder.Entity<CFFPartDisp>()
    .HasRequired(cpd => cpd.CFFPart)
    .WithMany(cp => cp.CFFPartDisps)
    .HasForeignKey(cpd => cpd.CFFPartID);

modelBuilder.Entity<CFFPartDisp>()
    .HasRequired(cpd => cpd.Disp)
    .WithMany(cp => cp.CFFPartDisps)
    .HasForeignKey(cpd => cpd.DispID);

modelBuilder.Entity<CFFPartDisp>()
    .HasMany(cpd => cpd.Experts)
    .WithMany(e => e.CFFPartDisps)
    .Map(m =>
    {
        m.MapLeftKey("CFFPartDispID");
        m.MapRightKey("ExpertID");
        m.ToTable("CFFpartDisPExpert");
    });
于 2012-12-30T21:59:16.943 に答える