首先使用流利的API在EF代码中一对一定义 [英] Define one-to-one in EF code first with fluent API
问题描述
我有两个拳头POCO(被任命者和案件):
I have two code fist POCOs (Appointee and Case):
public class Appointee
{
public int ID { get; set; }
public string ProfileID { get; set; }
public int? CaseID { get; set; }
public string FistName { get; set; }
public string LastName { get; set; }
public DateTime Dob { get; set; }
public string Ssn { get; set; }
public string GrantNumber { get; set; }
public virtual Case Case { get; set; }
}
public class Case
{
[Key]
public int CaseID { get; set; }
public string ProfileID { get; set; }
public int PSCStatusID { get; set; }
public virtual Appointee Appointee { get; set; }
}
在我们的术语中,被任命者是个人资料的同义词。因此,我们被任命者的[Key]是ProfileID。
In our terminology Appointee is synonymous with Profile. So the [Key] of our Appointee is ProfileID.
被任命者不必分配个案,因此我将CaseID设置为可为null的int?
An appointee doesn't have to have a case assigned so I have CaseID set as nullable int - int?.
由此我得到了一个错误,例如,无法在Case和被任命者之间确定EndPoint。
From this I get the error, something like, EndPoint cannot be determined between Case and Appointee.
I认为问题出在凯斯。
ProfileID(约会对象的外键)应该是虚拟约会对象属性的导航属性。
但是我不认为它能理解导航道具不是AppointeeID,而是ProfileID。
I think the problem is in Case. ProfileID, the foreign key to Appointee, is supposed to be the navigation property for the Virtual Appointee Property. But I don't think it understands that the navigation prop is not AppointeeID but is ProfileID instead.
所以我将其放在DbContext中:
So I put this in the DbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Appointee>().HasKey(a => a.ProfileID);
modelBuilder.Entity<Case>().HasKey(c => c.CaseID);
modelBuilder.Entity<Case>().HasRequired(c => c.Appointee);
modelBuilder.Entity<Appointee>().HasOptional(a => a.Case);
}
现在我得到:无效的列名'Appointee_ProfileID'。
Now I get: Invalid column name 'Appointee_ProfileID'.
如何正确设置它。
推荐答案
最后解决了这个问题。
这里有几件事。
我习惯于使用AutoInc设置代理主键,因此我为被任命者和案例设置了自动收入ID。
Finally solved this. A couple of things here. I'm so used to setting up Surrogate Primary Keys with AutoInc that I set up autoinc IDs for Appointee and Case.
确实,被任命者的密钥应为
Really the Key for Appointee should be ProfileID.
接下来,在Entity Framework中,从属表中的外键必须与父表中的主键相同,EF将其称为
Next, in Entity Framework, a Foreign key in the dependent table has to be the same as the Primary Key in the parent table, which EF refers to as the principle.
因此,一旦我意识到我的密钥既是PK又是FK,必须是ProfileID,我就进行了以下修复。
So once I got it in my head that my keys are both, PK and FK, going to have to be ProfileID, I made the following fixes.
public class Appointee
{
[Key]
public string ProfileID { get; set; }
public string FistName { get; set; }
public string LastName { get; set; }
public DateTime Dob { get; set; }
public string Ssn { get; set; }
public string GrantNumber { get; set; }
public virtual Case Case { get; set; }
}
public class Case
{
[Key, ForeignKey("Appointee")]
public string ProfileID { get; set; }
public int PSCStatusID { get; set; }
[ForeignKey("ProfileID")]
public virtual Appointee Appointee { get; set; }
}
请注意Case的[Key,ForeignKey( Appointee)]属性我需要告诉EF任命人是原则。
Notice the [Key, ForeignKey("Appointee")] attribute on Case which I need to tell EF that Appointee is the principle.
这篇关于首先使用流利的API在EF代码中一对一定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!