Fluent NHibernate 映射(有条件的一对一)
我正在尝试“清理”设计不良的数据库结构(至少在我的 ORM 中)。
表结构是这样的:
Table: Members
memberID (int PK)
username (varchar)
Table: Addresses
addressID (int PK)
memberID (int, not set as a FK - awesome)
firstName (varchar)
lastName (varchar)
addressLine1 (varchar)
isBillingAddress (bit)
所以我创建了 2 个类(实体),一个用于客户,一个用于地址。
public class Customer
{
public virtual int CustomerID { get; set; }
public virtual string FirstName
{
get { return BillingAddress.FirstName; }
set { BillingAddress.FirstName = value; }
}
public virtual string LastName
{
get { return BillingAddress.LastName; }
set { BillingAddress.LastName = value; }
}
public virtual Address BillingAddress { get; set; }
public virtual Address ShippingAddress { get; set; }
}
public class Address
{
public virtual Customer Customer { get; set; }
public virtual int AddressID { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string AddressLine1 { get; set; }
public virtual string AddressLine2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string PostalCode { get; set; }
}
检索客户帐单地址的查询将为:
SELECT TOP 1 *
FROM dbo.Address
WHERE isBilling = 1
AND memberID = @memberID
每个客户只能有 1 个帐单和送货地址。 到目前为止,我的流畅类映射如下所示:
public class CustomerMapping : ClassMap<Customer>
{
public CustomerMapping()
{
Table("Members");
Id(m => m.CustomerID).Column("memberID");
Map(m => m.BillingAddress);
HasOne(x => x.BillingAddress).PropertyRef("memberID");
HasOne(x => x.ShippingAddress).PropertyRef("memberID");
}
}
我不确定是否应该使用 HasOne...它应该是一对一的映射。如何获取其中的“where”子句(IE WHERE Address.memberID = Members.customerID
)来区分计费和运输?另外,top 1 呢?我知道我也许可以使用 Join
但我没有看到一个流畅的函数来添加 where 子句。
不幸的是,修改数据库结构不是一个选择。
谢谢
I'm trying to "clean" a poorly designed database structure (at least in my ORM).
The table structure is something like this:
Table: Members
memberID (int PK)
username (varchar)
Table: Addresses
addressID (int PK)
memberID (int, not set as a FK - awesome)
firstName (varchar)
lastName (varchar)
addressLine1 (varchar)
isBillingAddress (bit)
So I created 2 classes (entities), one for a customer and one for an address.
public class Customer
{
public virtual int CustomerID { get; set; }
public virtual string FirstName
{
get { return BillingAddress.FirstName; }
set { BillingAddress.FirstName = value; }
}
public virtual string LastName
{
get { return BillingAddress.LastName; }
set { BillingAddress.LastName = value; }
}
public virtual Address BillingAddress { get; set; }
public virtual Address ShippingAddress { get; set; }
}
public class Address
{
public virtual Customer Customer { get; set; }
public virtual int AddressID { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string AddressLine1 { get; set; }
public virtual string AddressLine2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string PostalCode { get; set; }
}
The query to retrieve a customers billing address would be:
SELECT TOP 1 *
FROM dbo.Address
WHERE isBilling = 1
AND memberID = @memberID
There should only 1 billing and shipping address per customer.
So far, my fluent classmap looks like this:
public class CustomerMapping : ClassMap<Customer>
{
public CustomerMapping()
{
Table("Members");
Id(m => m.CustomerID).Column("memberID");
Map(m => m.BillingAddress);
HasOne(x => x.BillingAddress).PropertyRef("memberID");
HasOne(x => x.ShippingAddress).PropertyRef("memberID");
}
}
I'm not sure if I should even be using HasOne... it should be a one to one mapping. How can I get the "where" clause in there (I.E. WHERE Address.memberID = Members.customerID
) to distinguish between billing and shipping? Also, what about top 1? I know I can maybe use Join
but I dont see a fluent function to add the where clause.
Modifying the database structure is unfortunately not an option.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要 HasOne(),尽管听起来不错。 HasOne() 表示表共享主键。请改用 References()。
You don't want a HasOne(), though it sounds right. HasOne() means that the tables share primary keys. Use References() instead.