简体   繁体   中英

Fluent NHibernate Component Mapping issue

I have a SQL Server stored procedure that I'm executing using a query string that returns a table with the following columns (ItemNumber, ItemDescription, UPC, Price, Count) representing the count of stores that an Item has for a given price. So Item A is 1.00 at 50 stores and 2.00 at 55 stores.

Here is the code for executing the 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);
}

Here are my objects I'm trying to map using 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; }
    }
}

Here is my 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");
            });
        }
    }
}

Right now I'm just doing this for one item for one price and I'm getting a PropertyNotFoundException: Could not find a setter for property 'ItemNumber' in class 'Report.Entities.RetailPrice'.

Why is the code trying to load this on the RetailPrice object instead of the Item object? Does it have anything to do with running the stored procedure as a Fetch query?

Once I get that solved, how can I go about making the Item.Upc part of the Id on the RetailPriceMap?

Your built an entity for the retail price with a complete mapping. AliasToBean transformer does not use mappings but instead mapps columns to properties by convention. Change Transformer to AddEntity:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM