I cannot figure out how to configure my mappins with Fluent ๐
Here is my situation:
I have an Element object, which (in theory) should have a one-to-one relationship with a Search object. However, the Search object should have a one-to-many relationship with itself.
In other words, an Element object can have at most 1 Search object (can also have none), and a Search object, has a collection of other Search objects.
I started with the following mappings. They create the correct DB model I expected and can save fine. Problems start when retrieving data…
public class ElementMap : ClassMap< Element >
{
public ElementMap()
{
Schema( "dbo" );
Table( "Element" );
LazyLoad();
Id( x => x.Id )
.Column( "Id" )
.CustomType( "Int32" )
.Access.Property()
.CustomSqlType( "int" )
.Not.Nullable()
.UnsavedValue( 0 )
.GeneratedBy.Identity();
HasOne( x => x.Search )
.Cascade.All()
.Not.LazyLoad();
}
}
and
public class SearchMap : ClassMap< Search >
{
public SearchMap()
{
Schema( "dbo" );
Table( "Search" );
LazyLoad();
Id( x => x.Id )
.Column( "Id" )
.CustomType( "Int32" )
.Access.Property()
.CustomSqlType( "int" )
.Not.Nullable()
.UnsavedValue( 0 )
.GeneratedBy.Identity();
ReferencesAny( x => x.Parent )
.IdentityType< int >()
.MetaType< string >()
.EntityTypeColumn( "ParentType" )
.EntityIdentifierColumn( "ParentId" )
.AddMetaValue< Element >( "E" )
.AddMetaValue< Search >( "S" );
HasMany( x => x.Searches )
.Table( "Search" )
.KeyColumn( "ParentId" )
.Where( "ParentType = 'S'" )
.Cascade.All()
.LazyLoad();
}
}
So as said, the model looks correct, the Element table contains 1 column for the ID, the Search table contains an ID column, a ParentType column set to “E” if the parent is an Element object and set to “S” if the parent is a Search object, and finally a ParentId that references the ID of the parent.
Somehow it seems to be fine and makes sense (at least to me :P).
Here’s a sample of the data in the database:
Element table
Id
-----------
1
2
Search table
Id ParentType ParentId
----------- ---------- -----------
1 E 1
2 S 1
3 E 2
4 S 3
5 S 3
So here my first Element object contains a Search object that contains 1 Search object,
and the 2nd Element object contains a Search object that contains 2 search objects.
Now the problem is that when I retrieve data, the first Element object is correct, but the 2nd one isn’t. Its Search object is ID’d 2, where really it should be 3 (ID of the 2nd Search object whoses parent is marked as E in the database).
My guess is that I should somehow add a .Where( “ParentType = ‘E'” ) to the Element object mapping as I have done for the Search object, but there is no .Where() method on the HasOne() method (since it normally wouldn’t make sense to have one). So I really don’t know how to specify it… ๐
I hope this is clear enough as problems are always quite hard to explain ๐
Any help will be greatly appreciated since this is a pretty important project for me ๐
Thanks all!
Seb ๐
HasOne does not take into account the Any part of ReferenceAny hence it can’t be used here at all. The only Option i see is to map a private collection of searches with the appropriate where
ParentType = 'E'and handle the conversion to a reference in the propertySearch.