Fluent Nhibernate Mapping Problem
I'm fairly new to nhibernate and fluent nhibernate, I have an issue with associating a collection to a query. In the database I have a well and have 4 associated AFEs. The problem I'm having is that the collection of AFEs is not populating correctly. No matter what I do, I get 4 AFEs in the collection but they are all the same object. Is there anything obvious that I am doing wrong.
Thanks
PS. I don't have change access to the database that I'm hitting so I can't change the db to make this a true FK.
Parent
public WellHeaderMap()
{
Table("Well");
开发者_开发知识库 Id(x => x.PropertyNumber, "WELL_NUMBER");
Map(x => x.PropertyID, "PROPERTY_ID");
Map(x => x.Name, "WELL_NAME");
//AFEs is a IList<AFE>
HasMany(x => x.AFEs).Inverse().KeyColumn("Property_ID").PropertyRef("PropertyID").Fetch.Join();
}
Collection
public AFEMap()
{
Table("AFE");
Id(x => x.PropertyID, "PROPERTY_ID");
Map(x => x.AFETypeID, "AFE_TYPE_CODE");
Map(x => x.AFENumber, "AFE_NUMBER");
Map(x => x.IsDeleted, "DELETED_IND");
}
Query
var wellSearchCriteria = _session.CreateCriteria<WellHeader>()
.CreateAlias("AFEs", "afe")
.Add(Restrictions.Eq("PropertyNumber", id.ToString()))
//.Add(Expression.Eq("afe.AFETypeID", "01"))
//.Add(Expression.Eq("afe.IsDeleted", "N"));
I think you might have your WellHeader Id wrong, currently you have:
Id(x => x.PropertyNumber, "WELL_NUMBER");
Map(x => x.PropertyID, "PROPERTY_ID");
Probably should be:
Id(x => x.PropertyID, "PROPERTY_ID");
Map(x => x.PropertyNumber, "WELL_NUMBER");
The PropertyNumber and PropertyId were switched. However without seeing your schema, its hard to tell.
精彩评论