【问题标题】:Why is my entity type association being severed?为什么我的实体类型关联被切断了?
【发布时间】:2020-05-13 01:01:49
【问题描述】:

因此,我目前正在尝试为我们的开发人员使用测试信息为开发数据库播种,但我遇到了这个问题 -
实体类型“UserProfile”和“ProjectProgress”之间的关联已被切断,但该关系要么被标记为“必需”,要么被隐含地要求,因为外键不可为空。如果在切断所需关系时应删除依赖/子实体,则设置关系以使用级联删除。考虑使用“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>();

项目进度模型 -

    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; }

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

【问题讨论】:

    标签: c# sql entity-framework entity-framework-core ef-core-3.0


    【解决方案1】:

    查看cascade delete上的文档

    对于上面的第二个操作,如果外键不可为空,则将外键值设置为 null 无效。 (不可为空的外键等效于必需的关系。)在这些情况下,EF Core 会跟踪外键属性已被标记为 null,直到调用 SaveChanges,此时出现异常抛出,因为更改无法持久保存到数据库。这类似于从数据库中获取约束违规。

    尝试如下更改 ProjectProgress,使 AddBy 可以为空,如下所示:

    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; }
    }
    

    【讨论】:

    • AddBy 实际上是一个接口的一部分,它在我的代码中的许多地方都继承了,有没有办法在不修改很多代码的情况下解决这个问题?
    • 需要吗?这意味着,可以在没有配置文件的情况下存在 Progress 行吗?如果是,可以为空是要走的路。否则设置级联删除关系。
    猜你喜欢
    • 2020-12-21
    • 2011-04-12
    • 2021-04-18
    • 2020-05-21
    • 1970-01-01
    • 2023-03-15
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多