我是Nhibernate的新手。希望我能在这里找到答案。 这是我的班级:
public class PaymentAudit : EntityBase<int>, IAggregateRoot
{
public PaymentAudit() { }
public System.Guid PaymentStateId { get; set; }
public System.DateTime DateStamp { get; set; }
public Payment Trn { get; set; }
public PaymentSaga PaymentSaga { get; set; }
public ProcessState ProcessState { get; set; }
public PublishState PublishState { get; set; }
public System.Nullable<short> ChgCount { get; set; }
public string UserName { get; set; }
protected override void Validate()
{
}
}
这是我的映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Praweda.Interface.Framework.Model.Payment"
assembly="Praweda.Interface.Framework.Model">
<class name="PaymentAudit" table="PaymentAudit" lazy="false" >
<composite-id>
<key-property name="PaymentStateId" column="PaymentStateId" />
<key-property name="DateStamp" column="DateStamp" />
<key-property name="ProcessState" column="ProcessState" />
</composite-id>
<id name="PaymentState">
<generator class="identity" />
<column name="PaymentStateId" sql-type="uniqueidentifier" not-null="true" />
</id>
<id name="ProcessState">
<generator class="identity" />
<column name="ProcessState" sql-type="varchar" not-null="true" />
</id>
<id name="DateStamp">
<generator class="identity" />
<column name="DateStamp" sql-type="datetime" not-null="true" />
</id>
<many-to-one insert="false" update="false" lazy="false" name="PaymentStateId">
<column name="PaymentStateId" sql-type="uniqueidentifier" not-null="true" />
</many-to-one>
<property name="PaymentStateId">
<column name="PaymentStateId" sql-type="uniqueidentifier" not-null="true" />
</property>
<many-to-one insert="false" update="false" lazy="false" name="ProcessState">
<column name="ProcessState" sql-type="varchar" not-null="true" />
</many-to-one>
<property name="ProcessState">
<column name="ProcessState" sql-type="varchar" not-null="true" />
</property>
<many-to-one insert="false" update="false" lazy="false" name="PublishState">
<column name="PublishState" sql-type="varchar" not-null="false" />
</many-to-one>
<property name="PublishState">
<column name="PublishState" sql-type="varchar" not-null="false" />
</property>
<many-to-one insert="false" update="false" lazy="false" name="Trn">
<column name="Trn" sql-type="varchar" not-null="false" />
</many-to-one>
<property name="Trn">
<column name="Trn" sql-type="varchar" not-null="false" />
</property>
<property name="ChgCount">
<column name="ChgCount" sql-type="smallint" not-null="false" />
</property>
<property name="UserName">
<column name="UserName" sql-type="nvarchar" not-null="false" />
</property>
</class>
</hibernate-mapping>
当我尝试在会话工厂中添加存储库程序集时,这是代码
Configuration config = new Configuration();
config.AddAssembly("MyProject.Nhibernate.Repository");
log4net.Config.XmlConfigurator.Configure();
config.Configure();
_sessionFactory = config.BuildSessionFactory();
给我错误: 命名空间'urn:nhibernate-mapping-2.2'中的元素'class'在命名空间'urn:nhibernate-mapping-2.2'中具有无效的子元素'id'。
谢谢!
答案 0 :(得分:1)
您有一个composite-id
元素后跟一个id
元素。你不能兼得。
当表格中的主键跨越多列时,请使用composite-id
。
接下来,您只能拥有一个id
元素。它定义了如何唯一标识您的实体。
对于属性,您应该使用property
元素。
查看NHibernate文档可能会有所帮助: http://www.nhforge.org/doc/nh/en/#mapping