简体   繁体   English

EF6如何映射这个零或一对一的关系

[英]EF6 how to map this zero or one to one relationship

This is Code-First. 这是Code-First。

What's the correct Fluent or data annotation for this relationship? 这种关系的正确流利或数据注释是什么?

Getting EF mapping error: Unable to determine the principal end of an association between the types 'Model.SchoolInfo' and 'Model.School'. 获取EF映射错误:无法确定类型“Model.SchoolInfo”和“Model.School”之间关联的主要结束。 The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 必须使用关系流畅API或数据注释显式配置此关联的主要结尾。

If I add 如果我加

modelBuilder.Entity<School>().HasOptional(s => s.SchoolInfo).WithRequired(ss => ss.School);

I get this error: One or more validation errors were detected during model generation: School_SchoolInfo_Target: : Multiplicity is not valid in Role 'School_SchoolInfo_Target' in relationship 'School_SchoolInfo'. 我收到此错误:在模型生成期间检测到一个或多个验证错误:School_SchoolInfo_Target :: Multiplicity在关系'School_SchoolInfo'中的角色'School_SchoolInfo_Target'中无效。 Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'. 由于“从属角色”属性不是关键属性,因此从属角色的多重性的上限必须为“*”。

SQL Tables SQL表

table School
(
    int SchoolId not null PK
)

table SchoolInfo
(
    int SchoolInfoId not null PK IDENTITY
    int SchoolId not null FK UNIQUE
)

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
}

If you want to implement 1:1 mapping , this is how tables should look like: 如果要实现1:1映射 ,这就是表的外观:

学校

SchoolInfoes

And your entity classes in that case would look like this: 在这种情况下你的实体类看起来像这样:

public class School
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int SchoolId { get; set; }

    public virtual SchoolInfo SchoolInfo { get; set; }
}

public class SchoolInfo
{
    [ForeignKey("School")]
    public int SchoolInfoId { get; set; }

    public virtual School School { get; set; }
}

Your current table structure suggests 1:M mapping , and in order to map entity classes to it, you need to do slight changes: 您当前的表结构建议1:M映射 ,并且为了将实体类映射到它,您需要做一些小的更改:

public class School
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int SchoolId;

    public virtual IList<SchoolInfo> SchoolInfoes { get; set; }
}

public class SchoolInfo
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int SchoolInfoId { get; set; }

    public int SchoolId { get; set; }

    public virtual School School { get; set; }
}

You may consider reviewing your entity-relationship data model. 您可以考虑查看实体关系数据模型。 Try removing the SchoolInfoId field (you don't actually need it), and just using the school ID FK as a PK. 尝试删除SchoolInfoId字段(您实际上并不需要它),只需使用学校ID FK作为PK。 That will make your data model consistent. 这将使您的数据模型保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM