我试图制作多对一映射,但在添加文档的Assembly时失败了NHibernate.MappingException:
"无法编译映射文档:TestForElma.Models.Person.hbm.xml"DuplicateMappingException:重复集合角色映射TestForElma.Models.Person.Documents
public static ISession OpenSession()
{
var configuration = new Configuration();
var configurePath = HttpContext.Current.Server.MapPath(@"~ModelsNhibernatenhibernate.cfg.xml");
configuration.Configure(configurePath);
configuration.AddAssembly(typeof(Person).Assembly);
configuration.AddAssembly(typeof(Document).Assembly);
ISessionFactory sessionFactory = configuration.BuildSessionFactory();
new SchemaUpdate(configuration).Execute(true, true);
return sessionFactory.OpenSession();
}
这里是hbm.xml文件
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="TestForElma" namespace="TestForElma.Models">
<class name="Document" dynamic-update="true" >
<cache usage="read-write"/>
<id name="Id" type="int">
<generator class="native" />
</id>
<property name="Name" />
<property name="CreationDate" />
<property name="BinaryData" />
<many-to-one name="Author" class="Person"
column="AuthorId" cascade = "save-update"/>
</class>
</hibernate-mapping>
和
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="TestForElma" namespace="TestForElma.Models">
<class name="Person" dynamic-update="true" >
<cache usage="read-write"/>
<id name="Id" type="int">
<generator class="native" />
</id>
<property name="Name" />
<property name="Password" />
<bag name="Documents" inverse="true">
<key column="AuthorId"/>
<one-to-many class="Document"/>
</bag>
</class>
</hibernate-mapping>
和nhibernate.cfg
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Server=LocalSQLEXPRESS; database=DbName; Integrated Security=SSPI;
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2008Dialect
</property>
</session-factory>
</hibernate-configuration>
它也不会在数据库中创建新的表。
我开始使用Fluent Nhibernate,它很有效。