简体   繁体   中英

EF adding entity: The relationship could not be changed because one or more of the foreign-key properties is non-nullable

I'm trying to add some newly created entities to my database, but EF is throwing an InvalidOperationException with the message The relationship could not be changed because one or more of the foreign-key properties is non-nullable but it does not tell me which relationship is of issue.

Here are the relevant entities:

DatabaseContext OnModelCreating:

...

modelBuilder.Entity<Student>()
    .HasRequired(s => s.Home)
    .WithMany(s => s.StudentsInResidence)
    .HasForeignKey(s => s.HomeId);

modelBuilder.Entity<SignoutRequest>()
    .HasOptional(x => x.Destination)
    .WithMany(x => x.RequestsToHere)
    .HasForeignKey(x => x.DestinationId);

modelBuilder.Entity<SignoutRequest>()
    .HasRequired(x => x.Subject)
    .WithMany(x => x.SignoutRequests)
    .HasForeignKey(x => x.SubjectId);

...

SignoutRequest:

public class SignoutRequest
{
    public SignoutRequest()
    {
    }

    public int Id { get; set; }
    public string SubjectId { get; set; }
    ... 
    public int? DestinationId { get; set; }

    #region Custom Fields
    ...
    #endregion

    #region Virtual Properties
    ...
    public virtual Student Subject { get; set; }
    public virtual DeviceGroup Destination { get; set; }
    #endregion

}

Student:

public class Student : IWpEntity
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string EmailAddress { get; set; }
    public string PhoneNumber { get; set; }
    public int Form { get; set; }
    public int HomeId { get; set; }

    public virtual DeviceGroup Home { get; set; }
    public virtual ICollection<BlacklistEntry> BlacklistEntries { get; set; } 
    public virtual ICollection<SignoutRequest> SignoutRequests { get; set; }
}  

DeviceGroup:

public class DeviceGroup : IWpEntity
{
    public int Id { get; set; }
    public string FriendlyName { get; set; }
    public virtual ICollection<Device> Devices { get; set; }
    public virtual ICollection<Student> StudentsInResidence { get; set; }
    public virtual ICollection<SignoutRequest> RequestsToHere { get; set; } 
}  

IWpEntity is just a collection of methods.

And here is the code that throws the error:

_database.SubmittedRequests.Add(srq);
await _database.SaveChangesAsync();

srq is the SignoutRequest that I am trying to add to the database. It already has properties SubjectId and DestinationId set to valid IDs of objects already in the database.

I have absolutely no idea why, but I got rid of IWpEntity and it started working. I'm totally confused, and I have no idea why that would have any bearing on the Database. The design of the database in Sql Management Studio looks identical.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM