2010-07-13 44 views
0

我想更新從Asp.net中的子表視圖中引用的父表中的數據。在Asp.net和流利的nhibernate中更新父表的子表

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

使用上面的代碼,當我嘗試更新子表中的值時,它可以工作。但是,如果我嘗試保存父表中的更改,則不起作用。

你有什麼想法如何解決這個問題?

謝謝。

回答

0

好吧..我只是回答了我自己的問題。基本上只需添加父表的名稱即可工作。

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

耶!