简体   繁体   English

实体框架6更正外键关系

[英]Entity Framework 6 Correct a foreign key relationship

I'm trying to correct the relationship below. 我正试图纠正下面的关系。 For some reason, the primary key set on the ChildClass used a composite of the ParentClassId and the ChildClassId when it should have just used the ChildClassId. 出于某种原因,ChildClass上设置的主键在使用ChildClassId时使用了ParentClassId和ChildClassId的组合。

public partial class ParentClass
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ParentClassId { get; set; }

    public DateTime SomeDate { get; set; }

    public string SomeString { get; set; }

    public virtual ICollection<ChildClass> ChildClasses { get; set; }
}

public partial class ChildClass
{
    [Key, ForeignKey("ParentClass"), Column(Order = 1)]
    public int ParentClassId { get; set; }
    [Key, Column(Order = 2)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ChildClassId { get; set; }

    public virtual ParentClass ParentClass { get; set; }

    public DateTime SomeDate { get; set; }

    public string SomeString { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Entity<ParentClass>()
            .HasMany(e => e.ChildClasses)
            .WithRequired(e => e.ParentClass);
}

Thus far I have: 到目前为止,我有:

public partial class ParentClass
{
    public int ParentClassId { get; set; }

    public DateTime SomeDate { get; set; }

    public string SomeString { get; set; }

    public virtual ICollection<ChildClass> ChildClasses { get; set; }
}

public partial class ChildClass
{
    public int ChildClassId { get; set; }
    public int ParentClassId { get; set; }

    public virtual ParentClass ParentClass { get; set; }

    public DateTime SomeDate { get; set; }

    public string SomeString { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Entity<ParentClass>()
            .HasMany(e => e.ChildClasses)
            .WithRequired(e => e.ParentClass);
}

This provides a migration script which just drops and recreates the primary key as expected. 这提供了一个迁移脚本,它只是按预期方式删除并重新创建主键。

Now, when I load a parent, clear the Child objects and recreate them and then try to save the changes 现在,当我加载父对象时,清除子对象并重新创建它们,然后尝试保存更改

ParentClass p = await db.ParentClasses.Include("ChildClasses").SingleOrDefaultAsync(r => r.ParentClassId == 1).ConfigureAwait(false);
p.ChildClasses.Clear();

ChildClass c = new ChildClass { SomeDate = DateTime.Now, SomeString="some string" };
p.ChildClasses.Add(c);

await db.SaveChangesAsync().ConfigureAwait(false); //<< Error on this

I get the error: 我收到错误:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. 操作失败:无法更改关系,因为一个或多个外键属性不可为空。 When a change is made to a relationship, the related foreign-key property is set to a null value. 当对关系进行更改时,相关的外键属性将设置为空值。 If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. 如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。

The tables and associated keys, indexes etc look fine. 表和相关的键,索引等看起来很好。 The SaveChanges line doesn't get as far as the database so somewhere in the model (not visible to me) it seems to be not respecting the updated relationship. SaveChanges行没有达到数据库那么模型中的某个地方(对我来说不可见)似乎不尊重更新的关系。

EDIT: based on reading around I've also tried iterating through as per below and get the same error. 编辑:基于阅读我已经尝试迭代如下,并得到相同的错误。

foreach (ChildClass t in p.ChildClasses.ToList()) p.ChildClasses.Remove(t);

What am I missing? 我错过了什么?

Clear() method will not delete entities in ChildClasses collection. Clear()方法不会删除ChildClasses集合中的实体。 If you think that ChildClass can be without ParentClass you should make foreign key as nullable 如果您认为ChildClass可以没有ParentClass ,则应将外键设为可为空

public int? ParentClassId { get; set; } public int? ParentClassId { get; set; } then it should work public int? ParentClassId { get; set; }那么它应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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