Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer peopleโ€™s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer peopleโ€™s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:48:48+00:00 2026-06-09T14:48:48+00:00

I cannot figure out how to configure my mappins with Fluent :( Here is

  • 0

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 ๐Ÿ™‚

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:48:49+00:00Added an answer on June 9, 2026 at 2:48 pm

    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 property Search.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have another strange bug which I cannot figure out. I try to create
I cannot figure out why this doesn't work. It seems so simple. It should
I cannot figure out what my object instance names are because of how I
I cannot figure out how to get rid of errors that should not be
Cannot figure out, where to change EOF in PyCharm. My scripts start with :
I cannot figure out this positioning problem in Firefox. It doesn't seem to follow
I cannot figure out how to write the following query using the DbFinderPlugin 1.2.2
I cannot figure out why I get this error during check-in. I checked in
I cannot figure out how to convert this code from C# to VB.net. It
I cannot figure out what is obscuring my buttons. The first image shows the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.