[英]EF is mapping to the wrong keys on entities
When I run a linq query it's trying to map the SchoolInfo.SchoolInfoId to the SchoolId.SchoolId. 当我运行linq查询时,它试图将SchoolInfo.SchoolInfoId映射到SchoolId.SchoolId。
How do I define the correct mapping so it knows to map SchoolInfo.SchoolId to School.SchoolId? 如何定义正确的映射,使其知道将SchoolInfo.SchoolId映射到School.SchoolId?
This is Code-First. 这是代码优先的。
SQL Tables SQL表
table School
(
int SchoolId not null PK
)
table SchoolInfo
(
int SchoolInfoId not null PK
int SchoolId not null FK
)
Models 楷模
class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
int schoolId;
virtual SchoolInfo SchoolInfo;
}
class SchoolInfo
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
int schoolInfoId;
int schoolId;
virtual School School
}
modelBuilder.Entity<School>().HasOptional(a => a.SchoolInfo).WithRequired(a => a.School);
A more appropriate way to do is something like: 一种更合适的方法是:
Data Base: 资料库:
TABLE School (
INT SchoolId NOT NULL PK
)
TABLE SchoolInfo (
INT SchoolId NOT NULL PK -- FK
)
School Model: 学校模式:
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
SchoolInfo Model Option 1: SchoolInfo模型选项1:
public class SchoolInfo
{
[Key, ForeignKey("School")]
public int schoolId { get; set; }
public virtual School School { get; set; }
}
SchoolInfo Model Option 2: SchoolInfo模型选项2:
public class SchoolInfo
{
[ForeignKey("School")]
public int SchoolInfoId { get; set; }
public virtual School School { get; set; }
}
SchoolInfo Model Option 3: SchoolInfo模型选项3:
public class SchoolInfo
{
[Key]
public int schoolId { get; set; }
public virtual School School { get; set; }
}
// Relationship:
modelBuilder.Entity<School>().HasOptional(a => a.SchoolInfo).WithRequired(a => a.School);
An alternative way because of the restrictions you mentioned is something like: 由于您提到的限制,另一种方法是:
Your actual Data Base: 您的实际数据库:
TABLE School (
INT SchoolId NOT NULL PK
)
TABLE SchoolInfo (
INT SchoolInfoId NULL PK
INT SchoolId NOT NULL FK -- WITH UNIQUE CONSTRAINT TO ENSUERE ONE TO ONE
)
School Model: 学校模式:
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
SchoolInfo Model Option 1: SchoolInfo模型选项1:
public class SchoolInfo
{
public int schoolInfoId { get; set; }
[Key]
public int schoolId { get; set; }
public virtual School School { get; set; }
}
// Relationship:
modelBuilder.Entity<School>().HasOptional(a => a.SchoolInfo).WithRequired(a => a.School);
SchoolInfo Model Option 2 (I did not test it): SchoolInfo模型选项2(我没有对其进行测试):
public class SchoolInfo
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolInfoId { get; set; }
[ForeignKey("School")]
public int schoolId { get; set; }
public virtual School School { get; set; }
}
// Relationship:
modelBuilder.Entity<School>().HasOptional(a => a.SchoolInfo).WithRequired(a => a.School);
You can see: 您可以看到:
http://www.entityframeworktutorial.net/entity-relationships.aspx http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx http://www.entityframeworktutorial.net/entity-relationships.aspx http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.