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!)?
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.
(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.
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With