Fluent NHibernate 映射(有条件的一对一)

发布于 2024-10-05 20:51:29 字数 2097 浏览 9 评论 0原文

我正在尝试“清理”设计不良的数据库结构(至少在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀟灑尐姊 2024-10-12 20:51:29

您不需要 HasOne(),尽管听起来不错。 HasOne() 表示表共享主键。请改用 References()。

You don't want a HasOne(), though it sounds right. HasOne() means that the tables share primary keys. Use References() instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文