0

I'm done configuring the Fluent NHibernate application using multiple databases. When I run the application, I see that the session is creating the same tables in all the databases. I tried limiting the creation by using the following line of code in Mapping class

Schema("Monkey") <- in monkey ClassMap

Schema("Banana") <- in Banana ClassMap

The SQL Query Generated:

    if exists (select * from dbo.sysobjects where id = object_id(N'Banana.[Banan
a]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Banana.[Banana]

    if exists (select * from dbo.sysobjects where id = object_id(N'Monkey.[Monke
y]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table Monkey.[Monkey]

    create table Banana.[Banana] (
        Id INT IDENTITY NOT NULL,
       Color NVARCHAR(255) null,
       primary key (Id)
    )

At the above point the debugger caught an error saying:

The specified schema name "Banana" either does not exist or you do not have permission to use it.

Community
  • 1
  • 1
navule
  • 3,212
  • 2
  • 36
  • 54

1 Answers1

1

only add the the relevant tables to the sessionfactory for each database. I would seperate them per namespaces: "BananaDbMaps" and "MonkeyDbMaps"

foreach (var dataBase in dataBases)
{
    var model = new PersistenceModel();
    foreach (var type in Assembly.GetExecutingAssembly().GetExportedTypes())
    {
        if (!type.IsInterface && !type.IsAbstract && type.IsSubclassOf(typeof(IMappingProvider)) && type.Namespace.EndsWith(dataBase.Key + "DbMaps"))
        {
            model.Add(type);
        }
    }
    config = Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008.ConnectionString(dataBase.Value))
        .Mappings(m => m.UsePersistenceModel(model))
        .BuildConfiguration();
   _allFactories.Add(dataBase.Key, config.BuildSessionFactory());
}
Firo
  • 30,626
  • 4
  • 55
  • 94
  • Firo Thanks for the solution. It seems to be solving the problem which I understand from technical analysis, but the method UsePersistenceModel(model) is not being recognized in my solution. I tried to google it but I couldn't find the respective assembly for that. – navule Jul 02 '12 at 08:13
  • i used FNH 1.2 and it is part of the `m` in the lambda – Firo Jul 02 '12 at 08:30
  • yes, you are correct. And I named both the class maps as per your suggestion. But the loop is not going into the if condition `model.Add(type)` – navule Jul 02 '12 at 09:33
  • replace `IsSubclassOf` with `IsAssignableFrom` – Firo Jul 02 '12 at 12:52