在 Asp.net 和 Fluent nhibernate 中从子级更新父表
我正在尝试从 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();
}
}
使用上面的代码,当我尝试更新子表中的值时,它可以工作。但是,如果我尝试保存父表中的更改,则不起作用。
您有任何想法如何解决这个问题吗?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧..我刚刚回答了我自己的问题。基本上只需添加父级的表名称即可使其工作。
耶!!!
Ok.. I just answered my own question. Basically just add the table name of the parent for it to work.
Yay!!!