c# - 如何设置一个具有两个外键的实体?

标签 c# entity-framework

我有一个实体,它自己的表有两个外键。它看起来像这样:

public class SystemUser : BaseModel
{
    [Column("SystemUserId")]
    public override Guid ID { get; set; }

    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string MobilePhone { get; set; }
    public Guid? MobilePhoneProviderId { get; set; }
    public virtual MobilePhoneProvider MobilePhoneProvider { get; set; }
    public Guid? ManagerId { get; set; }
    public virtual SystemUser Manager { get; set; }
    public Guid? AdminAssistantId { get; set; }
    public virtual SystemUser AdminAssistant { get; set; }

    public bool IsActive { get; set; }
    public bool SendEmail { get; set; }

    public string FinaleUsername { get; set; }
    public string FinalePassword { get; set; }
    public int? FloatUserId { get; set; }

    public Guid? SystemUserTypeId { get; set; }
    public virtual SystemUserType SystemUserType { get; set; }
    public Guid? SystemUserJobFunctionId { get; set; }
    public virtual SystemUserJobFunction SystemUserJobFunction { get; set; }
    public Guid? SystemUserJobRoleId { get; set; }
    public virtual SystemUserJobRole SystemUserJobRole { get; set; }
    public Guid? SystemUserLocationId { get; set; }
    public virtual SystemUserLocation SystemUserLocation { get; set; }

    public virtual ICollection<SystemUserRole> Roles { get; set; }
}

注意 AdminAssistantManager 属性。但是它给了我以下错误:

Unable to determine the principal end of an association between the types 'SystemUser' and 'SystemUser'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

如果我删除其中一个(任一者,哪一个并不重要),它工作正常,但由于某种原因,它对同时拥有两者感到困惑。我尝试在每个属性上添加 ForeignKey 属性,并使用 Fluent API 进行如下配置:

modelBuilder.Entity<SystemUser>()
            .HasOptional(x => x.Manager)
            .WithMany()
            .HasForeignKey(x => x.ManagerId);

modelBuilder.Entity<SystemUser>()
            .HasOptional(x => x.AdminAssistant)
            .WithMany()
            .HasForeignKey(x => x.AdminAssistantId);

都没有效果。有什么办法可以做到这一点吗?

仅供引用,我没有使用“代码优先”。我正在手动编写数据库脚本,并且仅使用 EF 作为 ORM。不确定该信息是否相关,但我想无论如何我都会提供。

最佳答案

看起来您会想要包含类似的内容

ICollection<SystemUser> ManagedUsers {get; set;}
ICollection<SystemUser> AdminAssistedUsers {get; set;}

那么你应该能够做这样的事情:

modelBuilder.Entity<SystemUser>()
        .HasOptional(x => x.Manager)
        .WithMany(x => x.ManagedUsers)
        .HasForeignKey(x => x.ManagerId)
        .WillCascadeOnDelete(false);

modelBuilder.Entity<SystemUser>()
        .HasOptional(x => x.AdminAssistant)
        .WithMany(x => x.AdminAssistedUsers)
        .HasForeignKey(x => x.AdminAssistantId)
        .WillCascadeOnDelete(false);

关于c# - 如何设置一个具有两个外键的实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50139707/

相关文章:

linq - Entity Framework Linq - 如何获取包含所有数据的组

c# - EF Power Tools 被自引用表弄糊涂了

c# - 从 EF 查询获取 Id,然后使用它来删除记录

asp.net - 重写 Entity Framework 中的删除功能

c# - 在 C# 中调试命令行参数

c# - 存储过程,传递日期数组,以逗号分隔。转换错误

c# - 在 Windows 窗体的属性网格中正确显示自定义对象列表

c# - 基本的 while 循环 c#

c# - 为什么在实体中有私有(private)二传手

c# - ASP.Net 和 GetType()