Tomas Aschan
Tomas Aschan

Reputation: 60674

Working Fluent NHibernate mappings render invalid CreateSchema SQL

I've inherited a bunch of Fluent NHibernate mappings, which all currently work in the project they're in (i.e. the application uses them and works). Now I'm trying to build integration tests for some new mappings and thus use all of them - old and new - to generate the schema, using

new SchemaExport(Container.Resolve<Configuration>()).Create(true, true);

However, the SQL generated script that is executed has some invalid code. Most of the problem seems to stem from lines like in one of these two cases:

// x.Map is string
Map(x => x.Map).Column("Karta").Not.Nullable().Default("False");
// x.Active is bool
Map(x => x.Active).Column("Aktiv").Not.Nullable().Default("True");

What happens is, in the statement that creates this table, the row corresponding to the above mapping looks like this (they're actually on different types, and in different tables):

Karta NVARCHAR(255) default False  not null,
Aktiv BIT default True  not null,

where (obviously) False doesn't work, since there is no variable with that name, and booleans don't exist in SQL Server.

How do I solve this?

I don't want to change the mappings, as there is a large application that uses and depends on them - I don't know (and I don't want to have to find out at this moment) what subtle bugs are introduced if I, say, removes the default specification. If I can change "False" and "True" to something that works, though, that is totally OK - just as long as it is very obvious that it won't change any behavior somewhere else.

Upvotes: 1

Views: 268

Answers (1)

Michael Buen
Michael Buen

Reputation: 39463

It's ok to change the .Default, an app doesn't obtain the defaults from mappings, an app receive default value from property's language default (string = null, int = 0, bool = false, etc) if the property is not initialized in constructor or property initializer(C# 3).

FNH .Default only has a bearing on DDL creation, app behavior won't be affected.

It's ok to change them to this:

Map(x => x.Map).Not.Nullable().Default("'True'");
Map(x => x.Aktiv).Not.Nullable().Default("0");

I highly doubt your team tightly couple your app functionality to NH, it's way too much trouble obtaining an app's property's default from NH default or FNH default even there's an API to facilitate so. The best way to know is to ask your team if they did that.

Upvotes: 1

Related Questions