Fluent NHibernate 表每个子类保存
这是我的设置:
public class Parent
{
public virtual int Id { get; protected set; }
public virtual Property1 { get; set; }
}
public class Child : Parent
{
public virtual Property2 { get; set; }
}
public sealed class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Id( m => m.Id );
Map( m => m.Property1 );
}
}
public sealed class ChildMap : SubclassMap<Child>
{
public ChildMap()
{
KeyColumn( "ParentId" );
Map( m => m.Property2 );
}
}
这对于检索数据非常有用。我该如何保存这个呢?我想在附加到父级的数据库中创建一个新的子记录。如果我创建一个具有相同父值的新子类并保存,我会收到错误消息,指出具有相同标识符值的不同对象已与实体:Child 的会话:2 关联
我猜这是有道理的,但是如何创建一个在数据库中具有相同父级的新子级?
也许这是不可能的,我应该只做从父母到孩子的一对多。
This is my setup:
public class Parent
{
public virtual int Id { get; protected set; }
public virtual Property1 { get; set; }
}
public class Child : Parent
{
public virtual Property2 { get; set; }
}
public sealed class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Id( m => m.Id );
Map( m => m.Property1 );
}
}
public sealed class ChildMap : SubclassMap<Child>
{
public ChildMap()
{
KeyColumn( "ParentId" );
Map( m => m.Property2 );
}
}
This works great for retrieving data. How do I go about saving this though? I want to create a new child record in the database that's attached to the parent. If i create a new child class that has the same parent values and save, I get errors saying a different object with the same identifier value was already associated with the session: 2, of entity: Child
I guess that makes sense, but how do I create a new Child that has the same parent in the database?
Maybe this isn't possible and I should just do a one to many from the Parent to the Child.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您滥用继承。您无法更改对象的类型。
您可能想要一对一的。
You are misusing inheritance. You can't change the type of an object.
You probably want a one-to-one.