【问题标题】:Update parent table from child in Asp.net and fluent nhibernate从 Asp.net 和流利的 nhibernate 中的子更新父表
【发布时间】:2010-07-13 05:45:03
【问题描述】:

我正在尝试从 Asp.net 中的子视图更新子表中引用的父表中的数据。

我使用 Fluent Nhibernate 映射表。

在子表中,映射如下:

public class ChildMap: ClassMap<Child>
{
    public ChildMap()
    {
        Id(i => i.childID).Not.Nullable();
        Map(i => i.childValue1);
        Map(i => i.childValue2);
        Map(i => i.childValue3);
        References(i => i.parent, "parentID").Cascade.All();
    }

父表的映射:

public class ParentMap: ClassMap<Parent>
{
    public ParentMap()
    {
        Id(i => i.parentID).Not.Nullable();
        Map(i => i.parentValue1);
        Map(i => i.parentValue2);
        Map(i => i.parentValue3);
        HasMany(i => i.Child).KeyColumn("childID").Cascade.All().Inverse();

    }
}

在我的控制器中,它看起来像这样......

    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {

        try
        {
            using (var tr = UnitOfWork.CurrentUnitOfWork.BeginTransaction())
            {
                try
                {

                    var child= Registry.Childs.Get(id);

                    //This update works
                    UpdateModel(child, new[] { "childValue1", "childValue2", "childValue3" }, collection.ToValueProvider());

                    //This update on the parent doesn't work
                    UpdateModel(child.parent, new[] { "child.parent.parentValue1", "child.parent.parentValue2", "child.parent.parentValue3" }, collection.ToValueProvider());

                    tr.Commit();
                }
                catch (Exception)
                {
                    tr.Rollback();
                    throw;
                }
            }

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

使用上面的代码,当我尝试更新子表中的值时,它可以工作。但是,如果我尝试将更改保存在父表中,则不起作用。

你有什么办法解决这个问题吗?

谢谢。

【问题讨论】:

    标签: c# asp.net fluent-nhibernate unitofworkapplication


    【解决方案1】:

    好的.. 我刚刚回答了我自己的问题。基本上只需添加父级的表名即可。

    UpdateModel(child.parent, "Parent", new[] { "child.parent.parentValue1", "child.parent.parentValue2", "child.parent.parentValue3" }, collection.ToValueProvider());
    

    耶!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 2013-05-05
      相关资源
      最近更新 更多