在运行时使用 NHibernate 创建一对多关联时出错?
我遇到的情况是,实体模型的某些部分仅在运行时已知。为了以简单的方式创建查询,我想在创建 SessionFactory
之前在引用它们的项目上添加一个简单的一对多
关联。
我查看了 Ayende 博客中的这篇文章 和 扩展/修改 NHibernate 类运行时,但我遇到了一个错误:
未知的收集角色:MySandbox.Parent.Children
什么是收集角色以及如何指示 NHibernate 解析它,就好像它已添加到 hbm.xml 中一样?
我的测试:
using (var tx = this.session.BeginTransaction())
{
parent = new Parent() { Name = "Parent 1" };
this.session.Save(parent);
tx.Commit(); //<- The exception is thrown here
}
用于添加关联的代码:
// var cfg = <init nhibernate>
PersistentClass cls = cfg.GetClassMapping("MySandbox.Parent");
NHibernate.Mapping.Set value = new NHibernate.Mapping.Set(cls)
{
Role = cls.EntityName + ".Children",
IsGeneric = true,
GenericArguments = new Type[] { typeof(Child) }
};
NHibernate.Mapping.Property property = new NHibernate.Mapping.Property()
{
PropertyAccessorName = "noop",
Value = value,
Name = "Children",
PersistentClass = cls
};
cls.AddProperty(property);
我的类:
public class Parent
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
}
public class Child
{
public virtual int ID { get; set; }
public virtual int ParentID { get; set; }
public virtual string Name { get; set; }
}
我试图在运行时创建的关联映射:
<set name="Children" access="noop">
<key column="ParentID" />
<one-to-many class="MySandbox.Child, MySandbox"/>
</set>
如果我只是在 hbm.xml 文件中创建关联,它会完美工作。我还尝试将从 xml 创建的关联与使用上面代码创建的关联进行比较,但没有成功。
(注意:如果我不设置 Role
属性,则会抛出另一个异常“值不能为空。参数名称:键”。)
I have situation where a few parts of my entity model is known only at runtime. In order to create queries in a simple way I'd like to add a simple one-to-many
association on items referencing them before creating the SessionFactory
.
I've looked at this post in Ayende's blog and Extend/Modify NHibernate classes at runtime but I'm stuck at an error saying:
Unknown collection role: MySandbox.Parent.Children
What is a collection role and how do I instruct NHibernate to resolve it as if it was added in the hbm.xml?
My test:
using (var tx = this.session.BeginTransaction())
{
parent = new Parent() { Name = "Parent 1" };
this.session.Save(parent);
tx.Commit(); //<- The exception is thrown here
}
My code for adding the association:
// var cfg = <init nhibernate>
PersistentClass cls = cfg.GetClassMapping("MySandbox.Parent");
NHibernate.Mapping.Set value = new NHibernate.Mapping.Set(cls)
{
Role = cls.EntityName + ".Children",
IsGeneric = true,
GenericArguments = new Type[] { typeof(Child) }
};
NHibernate.Mapping.Property property = new NHibernate.Mapping.Property()
{
PropertyAccessorName = "noop",
Value = value,
Name = "Children",
PersistentClass = cls
};
cls.AddProperty(property);
My classes:
public class Parent
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
}
public class Child
{
public virtual int ID { get; set; }
public virtual int ParentID { get; set; }
public virtual string Name { get; set; }
}
The assocition mapping I'm trying to create at runtime:
<set name="Children" access="noop">
<key column="ParentID" />
<one-to-many class="MySandbox.Child, MySandbox"/>
</set>
It works perfect if I just create the association in the hbm.xml-file. I've also tried to compare a association created from xml with one created with the code above with no luck.
(Note: If I don't set the Role
-property another exception of "Value cannot be null. Parameter name: key" is thrown.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误是因为 MySandbox.Child 未映射。您需要映射它(通过创建 PersistentClass 等),类似于您对 Parent 类所做的操作。
The error is because MySandbox.Child is not mapped. You need to map it (by creating a PersistentClass, etc) similar to how you did for the Parent class.