【问题标题】:What is the best method for Required-to-Optional Relationship (One-to–Zero-or-One) in EF Core?EF Core 中必需到可选关系(一对零或一)的最佳方法是什么?
【发布时间】:2021-02-19 08:35:43
【问题描述】:

我已经将EntityFramework 6的项目实现到EntityFramework Core中。我必须将 EF6 关系模式迁移到 EF 核心。 我在下面找到了一些参考资料:

  1. Entity Framework Core zero-or-one to zero-or-one relation

但对 EF-Core 中的必需-可选关系没有任何想法。

Sample1.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:

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; }
}

请有人帮助我如何在 EF-Core 中使用流利的 api 做到这一点

【问题讨论】:

标签: c# entity-framework-core entity-framework-6


【解决方案1】:

你可以试试这个(参考:docs):

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

或者,您可以在您的 Student 模型中添加一个 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);
}

【讨论】:

  • 如果 Haskey 在配置中指定了我们所做的类似“modelBuilder.Entity().HasKey(sa => sa.StudentId)”。在 StudentAddress 模型中还有一个属性为 "public int StudentId { get; set; }";
  • @KarthicG 很抱歉无法理解您在该评论中的意思
  • 我用 2 个样本编辑了我的问题。您的回答对 sample1.cs 很有帮助。如果他们使用 HasKey() 方法,我需要回答 sample2.cs。
  • @KarthicG 我认为你在搞混。为什么要让 StudentId 成为 StudentAddress 表中的 PK? StudentAddress 应该有 StudentAddressId 作为 PK,也许 StudentId 作为 FK
  • 这是我的 sample2.cs 场景之一,所以问 EF 核心的预期结果是什么?
猜你喜欢
  • 2019-07-25
  • 2021-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
相关资源
最近更新 更多