简体   繁体   English

一对一实体框架

[英]One-to-one entity framework

I am doing the entity framework code first to set up my database. 我首先要做实体框架代码来建立数据库。

I have two classes where their relationships are one-to-one, Lecturer & LoginInfo . 我有两个类,它们之间的关系是一对一的, 讲师和LoginInfo

public class Lecturer
    {
        public int LecturerId { get; set; }
        public string FullName { get; set; }

        public int UserId { get; set; }
        public LoginInfo LoginInfo { get; set; }

    }

public class LoginInfo
{
    public int UserId { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public bool ChangePassword { get; set; }

    public Lecturer Lecturer { get; set; }
}

So for my entity framework, I have written this for the one-to-one relationship. 因此,对于我的实体框架,我是为一对一关系编写的。

modelBuilder.Entity<Lecturer>()
                .HasOne(input => input.LoginInfo)
                .WithOne(input => input.Lecturer)
                .HasForeignKey<Lecturer>(input => input.UserId);

From the code above, does it mean that Lecture has one LoginInfo, LoginInfo with one Lecturer and Lecturer has a UserId as a foreign key? 从上面的代码中,这是否意味着Lecture有一个LoginInfo,LoginInfo有一个Lecturer,而Lecturer具有UserId作为外键?

Another question would be, for this one to one relationship, do I have to write another set of code for LoginInfo like this: 另一个问题是,对于这种一对一的关系,我是否需要为LoginInfo编写另一组代码,如下所示:

modelBuilder.Entity<LoginInfo>()
                .HasOne(input => input.Lecturer)
                .WithOne(input => input.LoginInfo)
                .HasForeignKey<LoginInfo>(input => input.LecturerId);

I am just a beginner trying to learn, thanks for helping :). 我只是一个尝试学习的初学者,感谢您的帮助:)。

From the code above, does it mean that Lecture has one LoginInfo, LoginInfo with one Lecturer and Lecturer has a UserId as a foreign key? 从上面的代码中,这是否意味着Lecture有一个LoginInfo,LoginInfo有一个Lecturer,而Lecturer具有UserId作为外键?

Correct. 正确。 It also means that LoginInfo is the principal and Lecturer is the dependent end of the relationship. 这也意味着LoginInfo主体,Lecturer是关系的从属端。 Also since the FK property is not nullable, the relationship is required , ie in order to create Lecturer you have to create LoginInfo first, which is not associated with another Lecturer . 同样,由于FK属性不可为空,因此需要该关系,即,为了创建Lecturer您必须首先创建LoginInfo ,该LoginInfo与另一个Lecturer不关联。

Also note that since the UserId is not following the default convention for PK name, you should explicitly configure it as PK of the LoginInfo : 还要注意,由于UserId没有遵循PK名称的默认约定,因此您应该将其明确配置为LoginInfo PK:

modelBuilder.Entity<LoginInfo>()
    .HasKey(e => e.UserId);

Another question would be, for this one to one relationship, do I have to write another set of code for LoginInfo like this 另一个问题是,对于这种一对一的关系,我是否必须像这样为LoginInfo写另一组代码

No. Single relationship requires single configuration and single FK. 否。单一关系需要单一配置和单一FK。 If you do so, you would be defining a second relationship, which also would create circular dependency between the entity, which should be avoided in general. 如果这样做,您将定义第二个关系,这也会在实体之间创建循环依赖关系,通常应避免这种情况。 The first fluent configuration fully defines the desired relationship and is enough to handle loading related data and other CRUD operations. 第一个流利的配置完全定义了所需的关系,并且足以处理与加载相关的数据和其他CRUD操作。

For more info about terms, relationship types and configuration, see Relationships section of the EF Core documentation. 有关条款,关系类型和配置的更多信息,请参阅EF Core文档的“ 关系”部分。

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

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