C# EF映射到实体上的错误键
当我运行linq查询时,它试图将SchoolInfo.SchoolInfoId映射到SchoolId.SchoolId 如何定义正确的映射,以便它知道如何将SchoolInfo.SchoolId映射到School.SchoolId 这是代码优先 SQL表C# EF映射到实体上的错误键,c#,sql,.net,entity-framework,fluent,C#,Sql,.net,Entity Framework,Fluent,当我运行linq查询时,它试图将SchoolInfo.SchoolInfoId映射到SchoolId.SchoolId 如何定义正确的映射,以便它知道如何将SchoolInfo.SchoolId映射到School.SchoolId 这是代码优先 SQL表 table School ( int SchoolId not null PK ) table SchoolInfo ( int SchoolInfoId not null PK int SchoolId not nu
table School
(
int SchoolId not null PK
)
table SchoolInfo
(
int SchoolInfoId not null PK
int SchoolId not null FK
)
模型
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);
班级学校
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
国际学校ID;
虚拟学校信息;
}
班级学校信息
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
int schoolInfoId;
国际学校ID;
虚拟学校
}
modelBuilder.Entity().HasOptional(a=>a.SchoolInfo).WithRequired(a=>a.School);
更合适的方法是:
数据库:
TABLE School (
INT SchoolId NOT NULL PK
)
TABLE SchoolInfo (
INT SchoolId NOT NULL PK -- FK
)
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
)
学校模式:
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
学校信息模型选项1:
public class SchoolInfo
{
[Key, ForeignKey("School")]
public int schoolId { get; set; }
public virtual School School { get; set; }
}
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);
学校信息模型选项2:
public class SchoolInfo
{
[ForeignKey("School")]
public int SchoolInfoId { get; set; }
public virtual School School { get; set; }
}
学校信息模型选项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);
学校模式:
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
学校信息模型选项1:
public class SchoolInfo
{
[Key, ForeignKey("School")]
public int schoolId { get; set; }
public virtual School School { get; set; }
}
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);
公共班级学校信息
{
公共信息ID{get;set;}
[关键]
公共int schoolId{get;set;}
公共虚拟学校{get;set;}
}
//关系:
modelBuilder.Entity().HasOptional(a=>a.SchoolInfo).WithRequired(a=>a.School);
SchoolInfo模型选项2(我没有测试):
公共班级学校信息
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
公共信息ID{get;set;}
[外侨(“学校”)]
公共int schoolId{get;set;}
公共虚拟学校{get;set;}
}
//关系:
modelBuilder.Entity().HasOptional(a=>a.SchoolInfo).WithRequired(a=>a.School);
你可以看到:
更合适的方法是: 数据库:
TABLE School (
INT SchoolId NOT NULL PK
)
TABLE SchoolInfo (
INT SchoolId NOT NULL PK -- FK
)
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
)
学校模式:
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
学校信息模型选项1:
public class SchoolInfo
{
[Key, ForeignKey("School")]
public int schoolId { get; set; }
public virtual School School { get; set; }
}
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);
学校信息模型选项2:
public class SchoolInfo
{
[ForeignKey("School")]
public int SchoolInfoId { get; set; }
public virtual School School { get; set; }
}
学校信息模型选项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);
学校模式:
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
public class School
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int schoolId { get; set; }
public virtual SchoolInfo SchoolInfo { get; set; }
}
学校信息模型选项1:
public class SchoolInfo
{
[Key, ForeignKey("School")]
public int schoolId { get; set; }
public virtual School School { get; set; }
}
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);
公共班级学校信息
{
公共信息ID{get;set;}
[关键]
公共int schoolId{get;set;}
公共虚拟学校{get;set;}
}
//关系:
modelBuilder.Entity().HasOptional(a=>a.SchoolInfo).WithRequired(a=>a.School);
SchoolInfo模型选项2(我没有测试):
公共班级学校信息
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
公共信息ID{get;set;}
[外侨(“学校”)]
公共int schoolId{get;set;}
公共虚拟学校{get;set;}
}
//关系:
modelBuilder.Entity().HasOptional(a=>a.SchoolInfo).WithRequired(a=>a.School);
你可以看到:
标题上说,是否映射到实体上的错误键?你的意思是单/双否定吗?好的,澄清一下:)您是使用CodeFirst还是Model/DatabaseFirst?如果无法更改数据库。。。在这种情况下,您可以从
SchoolInfo.SchoolInfoId
中删除键
DataAnnotation,并将其放入SchoolInfo.SchoolId
?否,除非SchoolInfo.ShcoolId
字段具有一些唯一性约束,否则它不是唯一的。如果SchoolInfo
表在ShcoolId
字段中可以有多条具有相同值的记录,则最终将有N(多)个子项。标题是,是否映射到实体上的错误键?你的意思是单/双否定吗?好的,澄清一下:)您是使用CodeFirst还是Model/DatabaseFirst?如果无法更改数据库。。。在这种情况下,您可以从SchoolInfo.SchoolInfoId
中删除键
DataAnnotation,并将其放入SchoolInfo.SchoolId
?否,除非SchoolInfo.ShcoolId
字段具有一些唯一性约束,否则它不是唯一的。如果SchoolInfo
表在ShcoolId
字段中可以有多个具有相同值的记录,那么您将有N(多)个孩子。我查看了,我必须更新更多的代码才能将此列添加到数据库中,而这目前是不可能的。有没有一种方法可以让上面的内容与Fluent或数据注释一起工作?我看了一下,我必须更新更多的代码才能将此列添加到数据库中,这在目前是不可能的。有没有一种方法可以让上述内容与Fluent或数据注释一起使用?