【发布时间】:2012-03-16 21:46:28
【问题描述】:
最近我在 NHibernate 的一对多关系中找到了an example。映射如下:
<class name="Company" table="Company">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<version name="Version"/>
<property name="Name"/>
<set name="Customers" inverse="true" lazy="true" >
<key column="Id"/>
<one-to-many class="Customer"/>
</set>
</class>
<class name="Customer" table="Customer">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<version name="Version"/>
<property name="DateOfBirth"/>
<property name="FirstName"/>
<property name="Surname"/>
<many-to-one name="Company" column="CompanyId" cascade="all-delete-orphan"/>
</class>
这是类:
public class Company
{
private ISet<Customer> customers = new HashedSet<Customer>();
public int Id { get; set; }
public string Name { get; set; }
public int Version { get; set; }
public ISet<Customer> Customers
{
get { return customers; }
set { customers = value; }
}
}
public class Customer
{
public Company Company { get; set; }
public DateTime? DateOfBirth { get; set; }
public string FirstName {get;set;}
public int Id { get; set; }
public string Surname {get;set;}
public int Version { get; set; }
}
我不喜欢这段代码的地方在于,将父对象包含在子对象中,将整个 Company 对象包含在 Customer 对象中。这不是循环引用吗?我的意思是,当这个图加载时,我不能做类似 currentCustomer.Company.Customers.First().Company.Customers.Last() 的事情......等等?还是那样不行?
假设基础表 Customer 只有一个字符串列 CompanyName,我该如何映射它?如果我在 Customer 类中使用公共字符串 CompanyName 而不是公共公司 Company,映射将如何?
【问题讨论】:
-
为什么你将 inverse 属性设置为 true 有什么性能原因吗?
标签: c# nhibernate nhibernate-mapping