简体   繁体   English

EF Core 中必需到可选关系(一对零或一)的最佳方法是什么?

[英]What is the best method for Required-to-Optional Relationship (One-to–Zero-or-One) in EF Core?

I have implemented the project of EntityFramework 6 into EntityFramework Core.我已经将 EntityFramework 6 的项目实现到 EntityFramework Core 中。 I have to do migrate EF6 relationship pattern into EF core.我必须将 EF6 关系模式迁移到 EF 核心。 I found some references below:我在下面找到了一些参考资料:

  1. Entity Framework Core zero-or-one to zero-or-one relation Entity Framework Core 零或一到零或一关系

but didn't get any ideas on required-optional relationship in EF-Core.但对 EF-Core 中的必需-可选关系没有任何想法。

Sample1.cs:样品1.cs:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<StudentAddress>()
                .HasRequired(x => x.Student)
                .WithOptional(x => x.StudentAddress);
}

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }
}

public class StudentAddress
{
    public int StudentId { get; set; }
    public string State { get; set; }
    public virtual Student Student { get; set; }
}

Sample2.cs:示例 2.cs:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<StudentAddress>()
                .HasKey(sa => sa.StudentId);
    modelBuilder.Entity<StudentAddress>()
                .HasRequired(x => x.Student)
                .WithOptional(x => x.StudentAddress);
}

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }
}

public class StudentAddress
{
    public int StudentId { get; set; }
    public string State { get; set; }
    public virtual Student Student { get; set; }
}

Kindly someone help me that how to do that in EF-Core using fluent api请有人帮助我如何使用流利的 api 在 EF-Core 中做到这一点

You can try this (Ref.: docs ):你可以试试这个(参考: 文档):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<StudentAddress>()
                .HasOne(x => x.Student)
                .WithOne(x => x.StudentAddress).IsRequired(false);
}

Alternatively, you could add a FK StudentAddressId in your Student Model an have the type be nullable int?或者,您可以在您的Student Model 中添加一个 FK StudentAddressId并且类型是否可以为空int?

public class Student
{
    public int StudentId { get; set; }
    
    // This is the FK for StudentAddress.
    // Make it int? (nullable) if StudentAddress is optional
    public int? StudentAddressId { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }
}

public class StudentAddress
{
    public int StudentId { get; set; }

    public virtual Student Student { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Since the FK is nullable, you don't need the IsRequired(false)
    modelBuilder.Entity<StudentAddress>()
                .HasOne(x => x.Student)
                .WithOne(x => x.StudentAddress);
}

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

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