views:

1503

answers:

1

I have these classes:

public class FloorFill
{
    protected FloorFill(){}
    public virtual ProductCatalog Catalog { get; set; }
    public virtual Inventory BatchedItem { get; set; }
    public virtual Transaction Batch { get; set; }
    public virtual int ItemReference { get; set; }
    public virtual IList<InventoryLocation> BackstockLocations { get; set; }
}
public class InventoryLocation
{
    public InventoryLocation(){}
    public virtual int Id { get; set; }
    public virtual int ItemReference { get; private set; }
    public virtual Location Where { get; set; }
    public virtual int HowMany { get; set; }
}

I have a database view that aggregates Items by location and some other filtering. I would like to reference this view in the mapping to populate the FloorFill.BackstockLocations collection. What approach should I be using to get this collection populated? I would like for the collection to lazy load, but at this point, I'd just be happy to get data.

Here are the mapping files:

public class FloorFillMap : EntityBaseMap<FloorFill>
{        
    public FloorFillMap()
    {
        Map(x => x.ItemReference);
        References(x => x.Catalog, "ProductCatalogId")
                        .WithForeignKey();
        References(x => x.Batch, "TransactionId")
                .WithForeignKey()
                .Cascade.SaveUpdate();
        References(x => x.BatchedItem, "InventoryId")
                .WithForeignKey()
                .Cascade.SaveUpdate();
        HasMany(x => x.BackstockLocations)
            .KeyColumnNames.Add("ItemReference")
            .Inverse()
            .Cascade.None()
            .LazyLoad();
    }
}

public class InventoryLocationMap : ClassMap<InventoryLocation>
{
    public InventoryLocationMap ()
    {
        WithTable("InventoryLocations");
        Id(x => x.Id);
        References(x => x.Where, "LocationId")
            .FetchType.Join()
            .Cascade.None();
        Map(x => x.HowMany);
        ReadOnly();
    }
}

The resulting query is :

SELECT this_.Id as Id33_0_, this_.Created as Created33_0_, this_.ItemReference as ItemRefe3_33_0_, this_.Modified as Modified33_0_, this_.RowVersion as RowVersion33_0_, this_.ProductCatalogId as ProductC6_33_0_, this_.TransactionId as Transact7_33_0_, this_.InventoryId as Inventor8_33_0_ FROM [FloorFill] this_

A: 

Mapping a view is the same as mapping a table, as long as you don't try to update it.

What are you trying to do in this statement?

References(x => x.Where, "LocationId")
  .FetchType.Join().WithColumns("Id").Cascade.None();

The "LocationId" is the key column name, but the WithColumns call will overwrite that value.

An error or some other indication of what's happening or not happening would help.

James Gregory
I removed the WithColumns("Id"). The select statement is comparing InventoryLocations.ItemRefence to the FloorFill.Id and I need to map it to FloorFill.ItemRefence.
Barry
You shouldn't have the WithTableName, that can be determined from the map of the other entity. In your InventoryLocationMap you should remove the ColumnName on your Id as that's the primary key, and remove the Map(x => x.ItemReference) because you're mapping your foreign-key as a property. Now what happens?
James Gregory
I've edited the code listing to reflect my understanding of your suggested changes. This is not creating a Backstock location collection.
Barry

related questions