gpt4 book ai didi

c# - Fluent NHibernate 组件映射问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:46:15 25 4
gpt4 key购买 nike

我有一个 SQL Server 存储过程,我正在使用查询字符串执行该存储过程,该查询字符串返回一个表,其中包含以下列(ItemNumber、ItemDescription、UPC、Price、Count),代表给定项目的商店数量价格。所以项目 A 在 50 家商店是 1.00,在 55 家商店是 2.00。

下面是执行 Fetch 的代码:

private IEnumerable<RetailPrice> FetchRetailPrices(IUnitOfWork unitOfWork, string upc)
{
string StoredProcedure = "EXEC RetailPrices @UPC = :upc";
List<Parameter> parameters = new List<Parameter>
{
new Parameter("upc", upc)
};

return unitOfWork.GetRepository<RetailPrice>().Fetch(
StoredProcedure, parameters);
}

这是我尝试使用 Fluent NHibernate 映射的对象:

namespace Report.Entities
{
[Serializable]
public class Item
{
public virtual string Upc { get; protected internal set; }
public virtual int ItemNumber { get; protected internal set; }
public virtual string ItemDescription { get; protected internal set; }
}
}

namespace Report.Entities
{
[Serializable]
public class RetailPrice
{
public virtual Item ItemDetails { get; protected internal set; }
public virtual decimal Price { get; protected internal set; }
public virtual int Count { get; protected internal set; }
}
}

这是我的 map :

namespace Report.Mappings
{
public class RetailPriceMap : ClassMap<RetailPrice>
{
public RetailPriceMap()
{
Id(a => a.Price).Column("Price");
Map(a => a.Count).Column("Count");

Component(a => a.ItemDetails, b =>
{
b.Map(c => c.ItemNumber).Column("ItemNumber");
b.Map(c => c.ItemDescription).Column("ItemDescription");
b.Map(c => c.Upc).Column("UPC");
});
}
}
}

现在我只是以一个价格为一件商品做这件事,我得到了一个 PropertyNotFoundException:无法在类“Report.Entities.RetailPrice”中找到属性“ItemNumber”的 setter 。

为什么代码试图将其加载到 RetailPrice 对象而不是 Item 对象?它与将存储过程作为 Fetch 查询运行有什么关系吗?

一旦我解决了这个问题,我该如何着手使 Item.Upc 成为 RetailPriceMap 上 Id 的一部分?

最佳答案

您为零售价构建了一个具有完整映射的实体。 AliasToBean 转换器不使用映射,而是按照约定将列映射到属性。将 Transformer 更改为 AddEntity:

public IEnumerable<TEntity> Fetch(String sql,IEnumerable<Parameter> parameters)
{
return this.BuildQuery(sql, parameters).AddEntity(typeof(TEntity)).List<TEnti‌​ty>();
}

关于c# - Fluent NHibernate 组件映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27988419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com