Cómo limitar Entidades a una base de datos particular en una aplicación FluentNhibernate de base de datos múltiple

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.

preguntado el 30 de junio de 12 a las 13:06

1 Respuestas

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());
}

Respondido 02 Jul 12, 07:07

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. - ombligo

i used FNH 1.2 and it is part of the m en la lambda - fuego

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) - ombligo

reemplazar IsSubclassOf con IsAssignableFrom - fuego

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.