0
使用EF代码首先我有了被关联到单个模型对象多个属性的模型对象:EF CodeFirst多FK协会单个模型对象
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);
}
当我运行应用程序的COMPA ny2Info和Company3Info为空。我尝试在上下文中为每家公司设置一个新的DbSet实例,但我得到了Unsupported Exception
。
谢谢!
更新:这里是同一个问题的答案,但是这不是为我工作Entity Framework Code First - two Foreign Keys from same table
你的意思是他们是空的?我没有看到任何代码来填充数据。 –
@LeonCullens我更新了我的代码。 – Rich