简体   繁体   English

EF添加实体:由于一个或多个外键属性不可为空,因此无法更改关系

[英]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. 它已经具有属性SubjectIdDestinationId设置为数据库中已经存在的对象的有效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.

相关问题 EF 6.1.1-无法更改关系,因为一个或多个外键属性不可为空 - EF 6.1.1 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable EF 6:插入多个-无法更改关系,因为一个或多个外键属性不可为空 - EF 6: inserting multiple - The relationship could not be changed because one or more of the foreign-key properties is non-nullable 实体框架5无法更改该关系,因为一个或多个外键属性不可为空 - Entity Framework 5 The relationship could not be changed because one or more of the foreign-key properties is non-nullable 实体框架无法更改关系,因为一个或多个外键属性不可为空 - Entity Framework The relationship could not be changed because one or more of the foreign-key properties is non-nullable 在EF6中,什么可能导致错误无法更改关系,因为一个或多个外键属性不可为空 - In EF6 what can cause the error The relationship could not be changed because one or more of the foreign-key properties is non-nullable EF SaveChanges方法抛出:由于一个或多个外键属性不可为空,因此无法更改关系 - EF SaveChanges method throws: The relationship could not be changed because one or more of the foreign-key properties is non-nullable 由于一个或多个外键属性不可为空,因此无法更改该关系 - The relationship could not be changed because one or more of the foreign-key properties is non-nullable Pocos-无法更改关系,因为一个或多个外键属性不可为空 - Pocos - The relationship could not be changed because one or more of the foreign-key properties is non-nullable 操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系asdf - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable, asdf 操作失败:无法更改关系,因为一个或多个外键属性不可为空 - The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM