在所有级别的嵌套子级中包括顶级实体ID

时间:2018-01-05 02:34:13

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

在我的应用程序中,我计划有几个嵌套实体,其中包含多个级别的嵌套对象,顶级对象是我的通用用户类型。

我希望每个嵌套实体都包含顶层实体的ID,以使查询更加简单,但在文档中它只显示父/子关系,所以我不确定如何在Entityframework核心中构建它以便顶级实体ID包含在每个实体中。

我写了这个简单的类结构来表明我的意思。这是正确的方法吗,以便级联删除仍然可以正常工作?

public class AppUser
{
    public string Id { get; set; }
    // .. various entity specific properties
    public AppUserTypeA AppUserTypeA { get; set; }
}

public class AppUserTypeA
{
    public string Id { get; set; }
    // .. various entity specific properties

    public Profile Profile { get; set; }

    [Required]
    public string AppUserId { get; set; }
    [ForeignKey(nameof(AppUserId))]
    public AppUser AppUser { get; set; }
}

public class Profile
{
    public int Id { get; set; }
    // .. various entity specific properties

    public ICollection<ProfileElement> ProfileElements { get; set; }

    [Required]
    public string AppUserTypeAId { get; set; }
    [ForeignKey(nameof(AppUserTypeAId))]
    public AppUserTypeA AppUserTypeA { get; set; }

    [Required]
    public string AppUserId { get; set; }
    [ForeignKey(nameof(AppUserId))]
    public AppUser AppUser { get; set; }
}

public class ProfileElement
{
    public int Id { get; set; }
    // .. various entity specific properties

    [Required]
    public string ProfileId { get; set; }
    [ForeignKey(nameof(ProfileId))]
    public Profile Profile { get; set; }

    [Required]
    public string AppUserId { get; set; }
    [ForeignKey(nameof(AppUserId))]
    public AppUser AppUser { get; set; }
}

0 个答案:

没有答案
相关问题