Reputation: 82186
Question regarding fluent nHibernate table mappings:
Apparently I was under the mistaken impression, that with an ORM tool, such as nHibernate, one could gain database independence - at least in table creation.
Now, I have an automated tool ( http://nmg.codeplex.com ), which creates fluent nHibernate mappings for me. It also works fine (though only if ALL tables have a primary key...)
Here a good representative example for such a mapping as it is created by the program
public class ELMAH_ErrorMap : ClassMap<ELMAH_Error>
{
public ELMAH_ErrorMap()
{
Table("ELMAH_Error");
LazyLoad();
Id(x => x.ErrorId).GeneratedBy.Assigned().Column("ErrorId");
Map(x => x.Application).Column("Application").Not.Nullable().Length(60);
Map(x => x.Host).Column("Host").Not.Nullable().Length(50);
Map(x => x.Type).Column("Type").Not.Nullable().Length(100);
Map(x => x.Source).Column("Source").Not.Nullable().Length(60);
Map(x => x.Message).Column("Message").Not.Nullable().Length(500);
Map(x => x.User).Column("User").Not.Nullable().Length(50);
Map(x => x.StatusCode).Column("StatusCode").Not.Nullable();
Map(x => x.TimeUtc).Column("TimeUtc").Not.Nullable();
Map(x => x.Sequence).Column("Sequence").Not.Nullable();
Map(x => x.AllXml).Column("AllXml").Not.Nullable().Length(1073741823);
}
}
Apparently, problem #1 was this line
Map(x => x.User).Column("User").Not.Nullable().Length(50);
Because user is a protected keyword in MS-SQL (and postgre as well). So schema-export failed with syntax error near "User".
Now, obviously this is solvable by enclosing the column (or table) name in the respective escape characters ([] for MS-SQL, "" for PostGreSQL)
So I changed
Map(x => x.User).Column("User").Not.Nullable().Length(50);
to
Map(x => x.User).Column("[User]").Not.Nullable().Length(50);
and it worked fine.
Then on PostGre, I had to change
Map(x => x.User).Column("[User]").Not.Nullable().Length(50);
to
Map(x => x.User).Column("\"User\"").Not.Nullable().Length(50);
because postgre uses this other escape sequence...
So now, do I have to create a separate mapping for every db system ? (MySQL has another escape character as well..., wondering what FireBird uses) ARGH ?!?
And another problem that exists as well: For every table & column that isn't enlosed in quotation marks, PostGre changes the table/column name to lowercase, which is "fine" (but only because it isn't case sensitive), but if the name is encosed in quotation marks, it becomes case-sensitive...
Then, I do have some 200 views to port... I don't actually want to have to write a parser just to enclose every column of every view (as well as table and view names) in whatever quotes used by whatever db...
Is there a way to tell fluent/nHibernate to escape the table & column names for me (either always, but preferably only if necessary) ?
The funny thing is, my own DB isn't actually the problem (I prefix all of my 200 tables with T_ and every column with some table id), the problem are all those crappy 3rd party components (like ELMAH or Session/Membership providers, etc.), that have been written by people who... [self censored]
Upvotes: 2
Views: 4283