为什么我的实体类型关联被切断了?

Why is my entity type association being severed?

所以我目前正在尝试为我们的开发人员在开发数据库中添加测试信息,但我 运行 陷入了这个问题 -
实体类型之间的关联 'UserProfile' 和 'ProjectProgress' 已被切断,但该关系被标记为 'Required' 或隐式需要,因为外键不可为空。如果在断开所需关系时应删除 dependent/child 实体,则设置关系以使用级联删除。考虑使用 'DbContextOptionsBuilder.EnableSensitiveDataLogging' 查看键值。

用户配置文件模型 -

        public int UserProfileId { get; set; }
        public UserProfileStatus UserProfileStatusId { get; set; } = UserProfileStatus.Active;
        public SiteRole SiteRoleId { get; set; }
        [MaxLength(100)]
        public string Username { get; set; }
        [MaxLength(50)]
        public string Password { get; set; }
        [MaxLength(100)]
        public string FirstName { get; set; }
        [MaxLength(100)]
        public string LastName { get; set; }
        [MaxLength(20)]
        public string Phone { get; set; }
        [MaxLength(150)]
        public string Email { get; set; }
        public DateTime PasswordChangeDt { get; set; } = DateTime.UtcNow;
        public DateTime? LastLoginDt { get; set; }
        public int InvalidLogins { get; set; }
        public bool Locked { get; set; }
        public DateTime? LockoutEnd { get; set; } //local copy of auth db value
        public bool Analyst { get; set; }
        public bool Reviewer { get; set; }
        public bool Broker { get; set; }
        public bool EmailGlobalStatusAlerts { get; set; }
        public bool EmailReviewerStatusAlerts { get; set; }
        public bool EmailAssignedStatusAlerts { get; set; }
        public bool Active { get; set; }
        public int? BankId { get; set; }

        public int AddBy { get; set; }
        public DateTime AddDt { get; set; } = DateTime.UtcNow;
        public int ModBy { get; set; }
        public DateTime ModDt { get; set; } = DateTime.UtcNow;

        public virtual Bank Bank { get; set; }

        [InverseProperty(nameof(ReviewerAnalyst.Analyst))]
        public virtual ICollection<ReviewerAnalyst> AnalystReviewers { get; set; } = new 
        List<ReviewerAnalyst>();
        [InverseProperty(nameof(DocumentAccessLog.AccessUser))] 
        public virtual ICollection<DocumentAccessLog> DocumentAccessLogs { get; set; } = new 
        List<DocumentAccessLog>();
        public virtual ICollection<EmailLog> EmailLogs { get; set; } = new List<EmailLog>();
        public virtual ICollection<Note> Notes { get; set; } = new List<Note>();
        [InverseProperty(nameof(Project.Analyst))]
        public virtual ICollection<Project> ProjectAnalyst { get; set; } = new List<Project>();
        [InverseProperty(nameof(Project.Reviewer))]
        public virtual ICollection<Project> ProjectReviewer { get; set; } = new List<Project>();
        [InverseProperty(nameof(ReviewerAnalyst.Reviewer))]
        public virtual ICollection<ReviewerAnalyst> ReviewerAnalysts { get; set; } = new 
        List<ReviewerAnalyst>();
        public virtual ICollection<TimeOff> TimeOffs { get; set; } = new List<TimeOff>();
        [InverseProperty(nameof(TimeOff.Approver))]
        public virtual ICollection<TimeOff> TimeOffApprovals { get; set; } = new List<TimeOff>();
        public virtual ICollection<UserEmailType> UserEmailTypes { get; set; } = new 
        List<UserEmailType>();
        public virtual ICollection<UserRole> UserRoles { get; set; } = new List<UserRole>();

ProjectProgress 模型 -

    public int ProjectProgressId { get; set; }
    public int ProjectId { get; set; }
    public int ProgressId { get; set; }
    [MaxLength(250)]
    public string Notes { get; set; }
    public bool Complete { get; set; }
    public int AddBy { get; set; }
    public System.DateTime AddDt { get; set; }

    public virtual Project Project { get; set; }
    public virtual Progress Progress { get; set; }
    [ForeignKey("AddBy")]
    public virtual UserProfile UserProfile { get; set; }

我不知道是否需要帮助解决此问题,但请告诉我,我会更新问题。

查看 cascade delete

上的文档

For the second action above, setting a foreign key value to null is not valid if foreign key is not nullable. (A non-nullable foreign key is equivalent to a required relationship.) In these cases, EF Core tracks that the foreign key property has been marked as null until SaveChanges is called, at which time an exception is thrown because the change cannot be persisted to the database. This is similar to getting a constraint violation from the database.

尝试按如下方式更改 ProjectProgress,使 AddBy 可以为 null,如下所示:

public calss ProjectProgress {
    public int ProjectProgressId { get; set; }
    public int ProjectId { get; set; }
    public int ProgressId { get; set; }
    [MaxLength(250)]
    public string Notes { get; set; }
    public bool Complete { get; set; }
    public int? AddBy { get; set; }
    public System.DateTime AddDt { get; set; }

    public virtual Project Project { get; set; }
    public virtual Progress Progress { get; set; }
    [ForeignKey("AddBy")]
    public virtual UserProfile UserProfile { get; set; }
}