简体   繁体   English

配置具有多个列的 EF Core 一对一关系(一个具有常量值)

[英]Configure an EF Core one-to-one relationship with multiple columns (one with a constant value)

Say I have a storage table/class and a folder table/class.假设我有一个存储表/类和一个文件夹表/类。 A storage always has one root folder (which is created when the storage is created and will exist until the storage is deleted), and a folder can have zero or more children.一个存储总是有一个根文件夹(它在创建存储时创建,并且在存储被删除之前一直存在),并且一个文件夹可以有零个或多个子文件夹。

Storage class:存储 class:

[Table("Storage")]
public class Storage
{
    public int Id { get; set; }

    public string Name { get; set; }

    public Folder RootFolder { get; set; }
}

Folder class:文件夹 class:

[Table("Folder")]
public class Folder
{
    public int Id { get; set; }

    public int StorageId { get; set; }

    public string Name { get; set; }

    public int? ParentFolderId { get; set; }

    public virtual Collection<Folder> Children { get; set; }
}

Storage table:存储表:

+----+-----------+
| Id |   Name    |
+----+-----------+
|  1 | Storage 1 |
|  2 | Storage 2 |
+----+-----------+

Folder table:文件夹表:

+----+-----------+------+----------------+
| Id | StorageId | Name | ParentFolderId |
+----+-----------+------+----------------+
|  1 |         1 |      | null           |
|  2 |         1 | foo  | 1              |
|  3 |         1 | bar  | 1              |
|  4 |         1 | baz  | 2              |
|  5 |         2 |      | null           |
+----+-----------+------+----------------+

As you can see, to get the root folder of a storage, you need to join using Storage.Id = Folder.StorageId and ParentFolderId is null .如您所见,要获取存储的根文件夹,您需要使用Storage.Id = Folder.StorageId and ParentFolderId is null With EF Core, how would one configure such a relationship in the OnModelCreating method?使用 EF Core,如何在OnModelCreating方法中配置这样的关系?

The following obviously does not work, since the ParentFolderId is null is not included in the relationship definition:以下显然不起作用,因为ParentFolderId is null不包含在关系定义中:

modelBuilder.Entity<Storage>(entity =>
{
    entity.HasOne(d => d.RootFolder)
        .WithOne()
        .HasForeignKey<Storage>(d => d.StorageId);
});

We are taking about 2 Foreign Key (FK) relations here我们在这里采用大约 2 个外键 (FK) 关系

  1. Between Storage and FolderStorageFolder之间
  2. Self reference relation of Folder Folder自引用关系

So, while defining relations, you need to define the 2 FK relations such that StorageId column (FK of Storage table in Folder) is not nullable and ParentFolderId ( Self reference FK) is nullable .因此,在定义关系时,您需要定义 2 个外键关系,使得StorageId column (FK of Storage table in Folder) is not nullable ,而ParentFolderId ( Self reference FK) is nullable

Finding the root folder is more of a query logic than a FK definition.查找根文件夹更多的是查询逻辑,而不是 FK 定义。 So, while querying, add a additional condition like .Where(s => s.ParentFolderId == null)因此,在查询时,添加一个附加条件,如.Where(s => s.ParentFolderId == null)

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

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