无法使用Entity Framework添加子记录

时间:2016-05-25 21:48:42

标签: database asp.net-core-mvc entity-framework-core

我不了解如何添加具有(或应该是)集合成员(父代)的实体框架的记录。当我将子类添加到数据库时,其外键不会使用父ID更新。我在模型中缺少什么,或者在添加子记录后我需要用上下文更新密钥?

我的模型类似于以下内容:

    DATE    30DAYSBACK  ID  INDICATOR   RUNNING30   EXPECTED
    3/2/16  2/1/16  ABC 1   3   3
    3/2/16  2/1/16  ABC 1   3   3
    3/2/16  2/1/16  ABC 1   3   3  
    3/7/16  2/6/16  ABC 1   7   7
    3/7/16  2/6/16  ABC 1   7   7
    3/7/16  2/6/16  ABC 1   7   7
    3/7/16  2/6/16  ABC 1   7   7
    3/8/16  2/7/16  ABC 1   10  10
    3/8/16  2/7/16  ABC 1   10  10
    3/8/16  2/7/16  ABC 1   10  10
    3/10/16 2/9/16  ABC 1   12  12
    3/10/16 2/9/16  ABC 1   12  12
    3/14/16 2/13/16 ABC 1   13  13
    3/15/16 2/14/16 ABC 1   14  14
    3/16/16 2/15/16 ABC 1   15  15
    3/21/16 2/20/16 ABC 1   16  16
    3/22/16 2/21/16 ABC 1   17  17
    3/23/16 2/22/16 ABC 1   19  19
    3/23/16 2/22/16 ABC 1   19  19
    3/25/16 2/24/16 ABC 1   20  20
    3/29/16 2/28/16 ABC 1   22  22
    3/29/16 2/28/16 ABC 1   22  22
    3/30/16 2/29/16 ABC 1   27  27
    3/30/16 2/29/16 ABC 1   27  27
    3/30/16 2/29/16 ABC 1   27  27
    3/30/16 2/29/16 ABC 1   27  27
    3/30/16 2/29/16 ABC 1   27  27
    3/31/16 3/1/16  ABC 1   29  29
    3/31/16 3/1/16  ABC 1   29  29
    4/1/16  3/2/16  ABC 1   31  31
    4/1/16  3/2/16  ABC 1   31  31
    4/4/16  3/5/16  ABC 1   32  29
    4/5/16  3/6/16  ABC 1   33  30
    4/13/16 3/14/16 ABC 1   34  27
    4/13/16 3/14/16 ABC 1   34  27
    4/13/16 3/14/16 ABC 1   34  27
    4/13/16 3/14/16 ABC 1   34  27
    4/15/16 3/16/16 ABC 1   35  24
    4/20/16 3/21/16 ABC 1   31  26
    4/20/16 3/21/16 ABC 1   31  26
    4/20/16 3/21/16 ABC 1   31  26
    4/25/16 3/26/16 ABC 1   31  25
    4/25/16 3/26/16 ABC 1   31  25
    4/25/16 3/26/16 ABC 1   31  25
    4/26/16 3/27/16 ABC 1   31  26
    4/27/16 3/28/16 ABC 1   34  29
    4/27/16 3/28/16 ABC 1   34  29
    4/27/16 3/28/16 ABC 1   34  29
    4/27/16 3/28/16 ABC 1   34  29
    4/28/16 3/29/16 ABC 1   35  30

在我添加子类的代码中,我执行了以下操作:

[Table("Parents")]
public class ParentClass{
  public int Id {get; set;}
  public virtual ICollection<ChildClass> ChildClasses {get; set;}
}

[Table("Children")]
public class ChildClass{
  public int Id {get; set;}
  public string Data {get; set;}
}

虽然添加了子项记录,但它们具有ParentClass的外键,但我没有看到如何在添加子记录后让Entity Framework更新数据库中的该字段。

我做错了什么?

0 个答案:

没有答案