带有非身份主键的EF核心延迟加载

时间:2020-07-31 09:58:30

标签: asp.net-core .net-core entity-framework-core

我正在使用.Net核心3.1和Entity Framework核心。 我有下面这样的实体

public class Task
{
    public int Id { get; set; }
    public string Title { get; set; }

    [ForeignKey("Project")]
    public int ProjectId { get; set; }

    [ForeignKey("Creator")]
    public int CreatorId { get; set; }

    public virtual Project Project { get; set; }
    public virtual Profile Creator { get; set; }
}

当我使用FindById获取任务时 我可以通过延迟加载来获取Project,但是即使CreatorId具有值,我也总是将Creator设为null。

这是项目和配置文件实体示例

public class Profile
{
    [Key]
    public int UserId { get; set; }
    
    public string Name { get; set; }

    public Designation? Designation { get; set; }    
}

public class Project
{
    public int Id { get; set; }
    
    public string Code { get; set; }
    
    public string Title { get; set; }
    
    public string Description { get; set; }
}

个人资料表UserId是我的主键,它不是自动标识,而是原始UserId表中的User的{​​{1}}。

将主键用作UserId而不是ID可能是问题所在吗?

0 个答案:

没有答案