Update parent table from child in Asp.net and fluent nhibernate

513 views Asked by At

I'm trying to update the data in the parent table that is referenced in the child table from the child's view in Asp.net.

I mapped the tables using Fluent Nhibernate.

In the child table, the mapping looks like this:

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();
    }

The parent table's mapping:

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();

    }
}

In my controller, it looks like this...

    [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();
        }
    }

With the codes above, when I try to update the values in the child table, it works. However, if I try to save the changes in the parent table, it doesn't work.

Do you have any ideas how to fix this?

Thanks.

1

There are 1 answers

0
ubiquitous On BEST ANSWER

Ok.. I just answered my own question. Basically just add the table name of the parent for it to work.

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

Yay!!!