[英]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. 我正在尝试向数据库中添加一些新创建的实体,但是EF抛出一条InvalidOperationException
消息, The relationship could not be changed because one or more of the foreign-key properties is non-nullable
但是它不能告诉我哪个关系很重要。
Here are the relevant entities: 以下是相关实体:
DatabaseContext OnModelCreating: 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: 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: 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. IWpEntity只是方法的集合。
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. srq
是我要添加到数据库中的SignoutRequest
。 It already has properties SubjectId
and DestinationId
set to valid IDs of objects already in the database. 它已经具有属性SubjectId
和DestinationId
设置为数据库中已经存在的对象的有效ID。
I have absolutely no idea why, but I got rid of IWpEntity and it started working. 我完全不知道为什么,但是我摆脱了IWpEntity并开始工作。 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. Sql Management Studio中的数据库设计看起来相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.