Fluent NHibernate join 处理多个字段问题
我目前正在研究如何将现有 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果名称是只读的那么
if the name is readonly then