简体   繁体   English

C#实体框架-将数据插入子表中时堆栈溢出

[英]C# Entity Framework - Stack Overflow when inserting data into child table

I am brand new to C# and Entity Framework. 我是C#和Entity Framework的新手。 I have been used these tutorials and StackOverflow to give me a better understanding. 我已使用这些教程和StackOverflow来使我更好地理解。

  1. http://developer.telerik.com/featured/working-with-databases-through-visual-studio/ http://developer.telerik.com/featured/working-with-databases-through-visual-studio/
  2. https://msdn.microsoft.com/en-us/data/jj193542 https://msdn.microsoft.com/en-us/data/jj193542

Using the Code-First approach, I have created a parent table and inserted data into the table no problem. 使用代码优先方法,我创建了一个父表并将数据插入该表中没问题。 When I had the parent table working correctly, I added the child table. 当父表正常工作时,我添加了子表。 I am now trying to insert data into that child table. 我现在正在尝试将数据插入该子表。 However, I always get a stack overflow error. 但是,我总是收到堆栈溢出错误。 I think that there is something wrong with my mapping, but I am not sure what. 我认为我的映射有问题,但是我不确定是什么问题。

The following is my code. 以下是我的代码。 This is where I define the database context and the objects for the database. 在这里定义数据库上下文和数据库对象。

public class DatabseContext : System.Data.Entity.DbContext
{
    public DbSet<Parent> Parents { get; set; }
    public DbSet<Child> Children { get; set; }
}

public class Parent
{
    private Guid parentIdVal;

    public Parent()
    {
        Children = new List<child>(); 
    }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid parentId
    {
        get { return parentIdVal; }
        set { parentIdVal = Guid.NewGuid(); }
    }

    [Required]
    [StringLength(4)]
    public string parentData { get; set; }

    public virtual List<Child> Children { get; set; }
}

public class Child
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid childId
    {
        get { return childId; }
        set { childId = Guid.NewGuid(); }
    }

    [ForeignKey("Parent")]
    public Guid parentId { get; set; }

    [Required]
    [StringLength(65)]
    public string childData { get; set; }

    public virtual Parent Parent { get; set; }
}

I have a different class what parses a file and inserts rows into each table. 我有一个不同的类来解析文件并将行插入到每个表中。 This is the code: 这是代码:

    private static void addParentRecord(String source)
    {
        using (var db = new DatabaseContext())
        {
            Parent parent = new Parent(); 
            addDataToParent(parent); 

            createChildren(source, parent);
            db.Parents.Add(parent);
            db.SaveChanges();
        }
    }

    private static void createChildren(String source, Parent parent)
    {
        Regex regex = new Regex(pattern);
        foreach (Match match in regex.Matches(source))
        {
            Child child = new child();
            child.childData = match.Groups[group].Value; 
            parent.Children.Add(child); 
        }
    }

So the code creates both the parent and the child tables. 因此,代码创建了父表和子表。 It creates both the parent and the child objects and populates the objects. 它创建父对象和子对象,并填充对象。 When I go to save the database changes, however, it continually calls the get ChildId and I get a stack overflow. 但是,当我保存数据库更改时,它会不断调用get ChildId并导致堆栈溢出。 There is some loop somewhere. 某处有一些循环。 I think it is because I don't have the parent-child mapping set up correctly. 我认为这是因为我没有正确设置父子映射。

Any help, pointers or links would be greatly appreciated. 任何帮助,指针或链接将不胜感激。 Thank you. 谢谢。

I figured it out. 我想到了。 I did three things. 我做了三件事。 I redid how I was creating the GUID, took out the lines DatabaseGenerated(DatabaseGeneratedOption.Identity) and I made sure that the ClassID method matched the class name with an Id added to the end(no extra 's or abbreviations). 我重做了如何创建GUID,取出DatabaseGenerated(DatabaseGeneratedOption.Identity)行,并确保ClassID方法将类名与末尾添加的ID匹配(没有额外的或缩写)。 To anyone having the same problem, my code now looks like this: 对于有相同问题的任何人,我的代码现在看起来像这样:

public class Parent
{
  public Parent()
  {
    Children = new List<child>(); 
    parentId = Guid.NewGuid(); 
  }

  [Key]
  public Guid parentId { get; set; }

  [Required]
  [StringLength(4)]
  public string parentData { get; set; }

  public virtual List<Child> Children { get; set; }
}

public class Child
{
  public Child()
  {
   childId = Guid.NewGuid();
  }

  [Key]
  public Guid childId { get; set; }

  [ForeignKey("Parent")]
  public Guid parentId { get; set; }

  [Required]
  [StringLength(65)]
  public string childData { get; set; }

  public virtual Parent Parent { get; set; }
}

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

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