【问题标题】:EntityFramework 5 CodeFirst Child Parent of the Same Type Not Updating/SavingEntity Framework 5 Code First Child Parent 的相同类型不更新/保存
【发布时间】:2013-03-11 04:37:36
【问题描述】:

我有一个名为 Section 的 class

public class Section
{
    public Section() { construct(0); }
    public Section(int order) { construct(order); }
    private void construct(int order) 
    {
        Children = new List<Section>();
        Fields = new List<XfaField>();
        Hint = new Hint();
        Order = order;
    }

    [Key]
    public int Id { get; set; }

    public int FormId { get; set; }

    public string Name { get; set; }

    [InverseProperty("Parent")]
    public List<Section> Children { get; set; }

    public List<XfaField> Fields { get; set; }

    public Section Parent { get; set; }

    public Hint Hint { get; set; }

    public int Order { get; private set; }


    #region Methods
    public void AddNewChild()
    {
        AddChild(new Section
        {
            Name = "New Child Section",
            FormId = FormId,
        });
    }
    private void AddChild(Section child)
    {
        child.Parent = this;

        if (Children == null) Children = new List<Section>();

        int maxOrder = -1;
        if(Children.Count() > 0) maxOrder = Children.Max(x => x.Order);

        child.Order = ++maxOrder;

        Children.Add(child);

        FactoryTools.Factory.PdfSections.Add(child);
    }
    // Other methods here
    #endregion
}

我正在尝试将新的孩子 Section 添加到已经存在的父母中,如下所示:

    private void AddChildSection()
    {
        var parent = FactoryTools.Factory.PdfSections.FirstOrDefault(x => x.Id == ParentId);

        if (parent == null) throw new Exception("Unable to create child because parent with Id " + ParentId.ToString() + " doesn't exist.");

        parent.AddNewChild();

        FactoryTools.Factory.SaveChanges();
    }

当我查看数据库时,我看到添加了一个新行,例如:

Id  Name                Parent_Id   Hint_Id FormId  Order
19  New Child Section   1           27      1       0

但是,当我加载父 Section 时,Children 属性始终为 Count 0,如下所示:

    public ActionResult EditSection(int formId, int sectionId)
    {
        var model = FactoryTools.Factory.PdfSections.FirstOrDefault(x => x.Id == sectionId);

        if (model == null || model.FormId != formId) model = new Section();

        //model.Children = FactoryTools.Factory.PdfSections.Where(x => x.Parent.Id == sectionId).ToList();

        return PartialView(model);
    }

当然,当我手动添加孩子时,他们就在那里(在上面的代码中,通过取消注释 model.Children = ... 行)

我习惯了 NHibernate 的做事方式,因此对上述看似简单的任务在 EntityFramework 中不起作用感到非常沮丧,我做错了什么?

【问题讨论】:

    标签: c# asp.net-mvc ef-code-first entity-framework-5


    【解决方案1】:

    实体框架不会急切地加载相关实体。尝试强制它包含孩子:

    var model = FactoryTools.Factory.PdfSections.Include("Children").FirstOrDefault(x => x.Id == sectionId);
    

    还有一个强类型重载,您可以将 lambda 传递给它:

    var model = FactoryTools.Factory.PdfSections.Include(s => s.Children).FirstOrDefault(x => x.Id == sectionId);
    

    【讨论】:

    • 什么程序集让我可以访问强类型版本?
    • Nm,刚找到System.Data.Entity
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2016-02-14
    • 2013-04-19
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多