Why is my entity type association being severed?

c# ef-core-3.0 entity-framework entity-framework-core sql

Question

So I am currently attempting to seed the dev database with test info for our developers, but I am running into this issue -
The association between entity types 'UserProfile' and 'ProjectProgress' has been severed but the relationship is either marked as 'Required' or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, then setup the relationship to use cascade deletes. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the key values.

User Profile Model -

        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 Model -

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

I don't know exactly is needed to help troubleshoot this issue, but let me know and I will update the question.

1
1
1/27/2020 4:04:32 PM

Accepted Answer

Check the documentation on 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.

Try changing the ProjectProgress as follows, by making the AddBy nullable like this:

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; }
}
1
1/27/2020 4:16:55 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow