Foo ごとに正確に 1 つの Bar があるいくつかのオブジェクトを格納したいと考えています。
次のような POCO オブジェクトがいくつかあります。
public class Foo
{
public int Id { get; set; }
public string FooProperty { get; set; }
public int BarId { get; set; }
public virtual Bar Bar { get; set; }
}
public class Bar
{
public int Id { get; set; }
public string BarProperty { get; set; }
public int FooId { get; set; }
public virtual Foo Foo { get; set; }
}
Foo と Bar は 1:1 の関係にあり、私が行った読み取りに基づいて、DbContext クラスで次のことを試しました。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Entity<Foo>()
.HasRequired(x => x.Bar)
.WithRequiredPrincipal(x => x.Foo);
base.OnModelCreating(modelBuilder);
}
バッキング ストアは SQL Server であり、これにより実際に 1 対 1 の関係を持つテーブルが作成されます。ただしBar
、からへの FK 関係は両方のテーブルFoo
のフィールドにありますが、テーブルのフィールドからテーブルのフィールドへの関係Id
であると予想されます。FooId
Bar
Id
Foo
EF は、両方のテーブルの PK (Id) フィールドの同期を維持することを決定したようで、基本的にBarId
/FooId
列を無視しています。
私は何を間違っていますか?