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

  • SEARCH
  • Home
  • 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 5987597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:50:17+00:00 2026-05-22T22:50:17+00:00

In my application, I need to talk to multiple databases. I am handling this

  • 0

In my application, I need to talk to multiple databases. I am handling this in NHibernate by creating one SessionFactory per database (I assume this is the correct thing to do). So I have two sets of models (one per database), and two sets of Fluent NHibernate ClassMap<> mappings. Both are in the same project (separated by namespace) and I’d like to keep it that way.

The problem comes when creating the SessionFactory. As far as I can see, Fluent NHibernate has basically two methods for adding mappings:

    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserClassMap>())
    .Mappings(m => m.FluentMappings.Add<UserClassMap>()

If I use the first overload, then my session factories get all the mappings for both databases. If I use the second, I have to specify each individual ClassMap. I’d like something like FluentMappings.AddFromNamespace(). Is there a way to do this?

  • 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-05-22T22:50:18+00:00Added an answer on May 22, 2026 at 10:50 pm

    It’s odd that FluentNHibernate supports this type of filtering for automapping, but not for ClassMaps. It shouldn’t be too hard to add this feature yourself, though, via the magic of extension methods. Try this:

    public static FluentMappingsContainer AddFromAssemblyOf<T>(
        this FluentMappingsContainer mappings,
        Predicate<Type> where)
    {
        if (where == null)
            return mappings.AddFromAssemblyOf<T>();
    
        var mappingClasses = typeof(T).Assembly.GetExportedTypes()
            .Where(x => (typeof(IMappingProvider).IsAssignableFrom(x)
                    || typeof(IExternalComponentMappingProvider).IsAssignableFrom(x))
                && where(x));
    
        foreach (var type in mappingClasses)
        {
            mappings.Add(type);
        }
    
        return mappings;
    }
    

    … and use it like this:

    m.FluentMappings.AddFromAssemblyOf<UserClassMap>(t => t.Namespace.StartsWith("One.Of.The.Two.Namespaces"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two application that need to talk to each other. App1 needs to
In my application i need the opposite to the date_of_talk__lte= as this results in
I have an application in C++, but it'll need to 'talk' to Java based
I'm building web application (in this context the client) which talk with a different
In my application I need show some info popup. This is just a text
I am developing one application and in that application I need to implement push
In an application I need to execute other programs with another user's credentials. Currently
I have a java application I need to pass some info to a C++
In certain areas in my application I need data from several tables in the
I'm working on a web-application and need to figure out the best way to

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.