EF Code First の使用 1 つのモデル オブジェクトに関連付けられた複数のプロパティを持つモデル オブジェクトがあります。
public class Job
{
public int Id { get; set; }
public int Company1Id { get; set; }
public int Company2Id { get; set; } // References CompanyId
public int Company3Id { get; set; } // References CompanyId
...
public virtual Company Company1Info { get; set; }
public virtual Company Company2Info { get; set; }
public virtual Company Company3Info { get; set; }
}
public class Company
{
public int CompanyId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
JobMap クラスの関係
this.HasRequired(t => t.Company1Info)
.WithMany()
.HasForeignKey(d => d.Company1Id);
this.HasRequired(t => t .Company2Info)
.WithMany()
.HasForeignKey(d => d.Company2Id);
this.HasRequired(t => t.Company3Info)
.WithMany()
.HasForeignKey(d => d.Company3Id);
私のレポでデータを取得する方法
public Job GetById(int id)
{
return _dbContext.Set<Job>()
.Include(t => t.Company1Info)
.Include(t => t.Company2Info)
.Include(t => t.Company3Info)
.First(x => x.Id == id);
}
アプリケーションを実行すると、Company2Info と Company3Info が null になります。コンテキスト内の各企業に対して新しい DbSet インスタンスを設定しようとしましたが、Unsupported Exception
.
ありがとう!
更新:これは同じ問題に対する回答ですが、これは私にとっては機能しませんEntity Framework Code First - 同じテーブルからの2つの外部キー