Fluent NHibernate join 处理多个字段问题

发布于 2024-12-26 03:52:30 字数 1468 浏览 3 评论 0原文

我目前正在研究如何将现有 .NET 4.0 MVC 3 Web 应用程序的数据访问层移植到实体框架。原因有很多,但最主要的一个是由于数千个存储过程,仅向表添加 1 个字段就会导致 30 - 50 次存储过程编辑!

我们使用的是 MS SQL Server 2008 R2,理想情况下,我们希望使用 NHibernate 和 Fluent 进行映射。

我已将问题简化为一个简单的示例:

想象一下以下 2 个表:

'产品' 表

ID (INT)
DefaultName (NVARCHAR(128))

'产品名称' 表

ProductID (INT)
Name (NVARCHAR(128))
Culture (VARCHAR(10))

产品表将包含产品列表,每个人都有一个默认的英文名称。产品名称表将包含产品的 ID 和许多翻译。

目前,使用存储过程,我们有以下内容:

SELECT Products.ID,
       ISNULL(ProductNames.Name, Products.DefaultName) AS Name
FROM Products
LEFT JOIN ProductNames ON ProductNames.ProductID = Products.ID AND ProductNames.Culture = @Culture;

注意:@Culture 被传递到过程

这始终确保返回具有本地化名称或默认(英语)名称的单个产品。

我的问题是:这可以在 Fluent NHibernate 的映射级别上完成吗?我一直在搜索“如何加入两列”几天,但找不到有效的解决方案。如果在如此成熟的框架中这是不可能的,这似乎很奇怪?

作为我一直在尝试的一个例子:

public class ProductMap : ClassMap<Product> {
  public ProductMap() {
    Id(p => p.Id);

    Join("ProductNames", pn => {
      pn.Optional()
        .KeyColumn("ProductID")
        .Map(p => p.Name);
    });
  }
}

但是,这会导致以下异常:

More than one row with the given identifier was found: 109, for class: Product

这是因为产品 109 有 5 个翻译,因此所有 5 个翻译不能映射到单个字符串。

我已经设法使用“HasMany<>”将所有翻译映射到产品内的列表的方法。然而,这不是我需要的。

Im currently researching how to port the Data Access Layer of an existing .NET 4.0 MVC 3 web application over to an entity framework. There are many reasons, but the primary one being due to thousands of stored procedures, adding just 1 field to a table results in 30 - 50 sproc edits!!

We are using MS SQL Server 2008 R2 and, ideally, we would like to use NHibernate and Fluent for mapping.

I have simplified the problem Im having into a simple example:

Imagine the following 2 tables:

'Products' Table

ID (INT)
DefaultName (NVARCHAR(128))

'Product Names' Table

ProductID (INT)
Name (NVARCHAR(128))
Culture (VARCHAR(10))

The Products table will contain a list of products, each of them will have a default, English, name. The Product Names table will contain the ID of the Product and many translations.

Currently, using stored procedures, we have the following:

SELECT Products.ID,
       ISNULL(ProductNames.Name, Products.DefaultName) AS Name
FROM Products
LEFT JOIN ProductNames ON ProductNames.ProductID = Products.ID AND ProductNames.Culture = @Culture;

Note: @Culture is passed into the procedure

This always ensures a single Product with either a localised name or default (English) name is returned.

My question is: Is this possible to do at the Mapping level of Fluent NHibernate? I have been searching for days on 'How to join on 2 columns', but cant find a solution which works. It would seem odd if this is not possible in such a mature framework?

As an example of what I have been experimenting with:

public class ProductMap : ClassMap<Product> {
  public ProductMap() {
    Id(p => p.Id);

    Join("ProductNames", pn => {
      pn.Optional()
        .KeyColumn("ProductID")
        .Map(p => p.Name);
    });
  }
}

However, this results in the following exception:

More than one row with the given identifier was found: 109, for class: Product

This is because product 109 has 5 translations and thus all 5 cannot be mapped to a single string.

I have managed to use the 'HasMany<>' method to map all translations into a List within a Product. However, this is not what I need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小猫一只 2025-01-02 03:52:30

如果名称是只读的那么

public class ProductMap : ClassMap<Product> {
    public ProductMap() {
        Id(p => p.Id);

        Map(p => p.Name).Formula("Select ISNULL(pn.Name, DefaultName) FROM ProductNames pn WHERE pn.ProductID = ID AND pn.Culture = '" + GetCUltureFromSomewhere() + "'");
    }
}

if the name is readonly then

public class ProductMap : ClassMap<Product> {
    public ProductMap() {
        Id(p => p.Id);

        Map(p => p.Name).Formula("Select ISNULL(pn.Name, DefaultName) FROM ProductNames pn WHERE pn.ProductID = ID AND pn.Culture = '" + GetCUltureFromSomewhere() + "'");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文