0

私はデータベース ファースト プロジェクトに取り組んでおり、EntityTypeConfigurationクラスを使用してモデルの Fluent 定義をセットアップしています (「リバース エンジニア コード ファースト」ツールを使用して初期クラスを取得しました)。

クラスの 1 つのセットには、2 つのオブジェクトを 1 対多の関係で関連付ける非標準キー n があります。クラスのセットの例を次に示します。

class Foo
{
    public int FooId {get; set;}
    public string SomeData {get; set;}
    public virtual ICollection<Bar> Bars {get; set;}
}

class Bar
{
    public int BarId {get; set;}
    public int ThisIntPointsToFooId {get; set;}
    public virtual Foo Foo {get; set;}
}

作成するとき、関係を両側から定義する必要がありますか? それとも、片側だけで作成し、もう一方がそれを取得することはできますかEntityTypeConfiguration<Foo>?EntityTypeConfiguration<Bar>

class FooMap : EntityTypeConfiguration<Foo>
{
    //(Snip other members)

    this.HasMany(t => t.Bars).WithRequired(t => t.Foo).HasForeignKey(t => t.ThisIntPointsToFooId);
}

class BarMap : EntityTypeConfiguration<Bar>
{
    //(Snip other members)

    this.HasRequired(t => t.Foo).WithMany(t => t.Bars).HasForeignKey(t => t.ThisIntPointsToFooId);
}
4

1 に答える 1