EF Core 2.0.0 一对一或零与 Fluent Api [英] EF Core 2.0.0 One to One-or-Zero with Fluent Api

查看:24
本文介绍了EF Core 2.0.0 一对一或零与 Fluent Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EF Core 2.0.0 的 Fluent Api 中,没有任何方法 HasRequiredHasOptional,我有两个模型、人员和员工:

 公共类 Person{公共 int Id { 获取;放;}公共 int EmployeeId { 获取;放;}公共虚拟员工员工{获取;放;}//选修的}公开课员工{公共 int Id { 获取;放;}公共 int PersonId { 获取;放;}公共虚拟人人{get;放;}//必需的}

<块引用>

  • Person 可能有员工:Optional
  • Employee 应该有 Person:Required

如何在数据库中应用这些约定.

解决方案

你可以只指定 int 吗?作为 EmployeeId 属性类型.

顺便说一句,无需将导航属性设为虚拟.

In Fluent Api at EF Core 2.0.0, there aren't any methods HasRequired and HasOptional, and i have tow Models, Person and Employee:

    public class Person
    {
        public int Id { get; set; }

        public int EmployeeId { get; set; }
        public virtual Employee Employee { get; set; } // Optional
    }

    public class Employee
    {
        public int Id { get; set; }

        public int PersonId { get; set; }
        public virtual Person Person {get; set; } // Required
    }

  • Person May to have Employee: Optional
  • Employee Should have Person: Required

How to apply these convetions in database.

解决方案

You could just specify int? as EmployeeId property type.

BTW, no need to make navigation properties virtual.

这篇关于EF Core 2.0.0 一对一或零与 Fluent Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆