Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent nHibernate: Use the same mapping files for tables with the same structure in different schemas

Tags:

This is my mapping class:

class MyTableMap : ClassMap<MyTable>
{
    public MyTableMap()
    {
        Schema("mySchema");
        Id(x => x.id);
        Map(x => x.SomeString);
    }
}           

This works fine for the Table ([mySchema].[MyTable]) in my first database.

But this table ("MyTable") exists in (actually a lot of) different databases, but for any reason the schema is always named different (this I dont have any control of):

So in the Database "OtherDB" there is the Table [SomeOtherSchema].[MyTable] with the same structure as [mySchema].[MyTable] in the first db.

For obvious reasons I dont want to create a different mapping class for every database.

So: Is there a way to change the schema of the mapping class so I just have to create one mapping class (Without using a singelton!)?

like image 413
Gener4tor Avatar asked Feb 11 '19 13:02

Gener4tor


People also ask

What is the difference between NHibernate and fluent NHibernate?

Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.

What is fluent NHibernate?

(Error Code: 241403) Fluent mapping is the namesake mapping style that Fluent NHibernate uses. It's a fluent interface that allows you to map your entities completely in code, with all the compile-time safety and refactorability that brings. The getting started guide has a good introduction to mapping with the fluent interface.

How do I map the properties of model classes in NHibernate?

Fluent NHibernate uses strongly typed C# classes to map the properties of the model classes to the corresponding fields of the database tables. Here's the mapping class named ProductMap.

What is the mapping language in NHibernate?

The mapping language is object-centric, meaning that mappings are constructed around persistent class declarations, not table declarations. Note that, even though many NHibernate users choose to define XML mappings by hand, a number of tools exist to generate the mapping document, even transparently at runtime.

What is NHibernate entity-name?

entity-name (optional - defaults to the class name): NHibernate allows a class to be mapped multiple times, potentially to different tables. See Section 5.3, “Mapping a class more than once” . It also allows entity mappings that are represented by dictionaries at the .Net level.


1 Answers

It seems like I have to use the "DefaultSchema". So I used this mapping code:

class MyTableMap : ClassMap<MyTable>
{
    public MyTableMap()
    {
        Id(x => x.id);
        Map(x => x.SomeString);
    }
}           

When I build the sessionFactory I have to set the DefaultSchema:

var configure = Fluently.Configure();
var dbConfig = MsSqlConfiguration.MsSql2012.ConnectionString("Data Source=" + dataSource +
                                                            ";Initial Catalog=" + database +
                                                            ";Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

//Here I can set the default schema used by my mappings
var dbConfigWithSchema = dbConfig.DefaultSchema(database);  
var fluentDb = configure.Database(dbConfigWithSchema);

var fluentMap = fluentDb.Mappings(mappings);
return fluentMap.BuildSessionFactory();
like image 151
Gener4tor Avatar answered Oct 05 '22 03:10

Gener4tor