Provide default value for string field using Fluent NHibernate

I am having a problem setting a default value for a string using Fluent Nhibernate. I have a database with a varchar field that is not null and I am trying to build some objects and then update them to the database, however, since the string can be null within .Net, I got a problem when I try to update object to the database.

I have Googled for a solution, but so far the word "Default" seems to show up in many related topics, so it's hard to filter out what I really want.

What I would like to do is set the default for the match to be an empty string so that the invocation code does not need to know that the database field is not null. So far, setting the mapping to .Default (string.Empty) hasn't worked, so I'm guessing I'm doing it wrong. I can fix this by adding a constructor call to set the field to an empty string in the entity's constructor, but it looks pretty frustrating, so I'm looking for a better way to do it if there is one in there.

So some of the code ...

Entity property (pretty vanilla):

public virtual string Notes { get; set; }

      

and Display:

Map(m => m.Notes).Default(string.Empty).Not.Nullable();

      

I've also tried using a literal empty string .Default ("") and putting Not.Nullable () before the default and none of them worked.

For now, I'm going to use a constructor call so that doesn't block me, but I'd like to find a better way to do it, if there is one.

Any help is greatly appreciated.

+3


source to share


2 answers


Set the value of your property to an empty string in your object's constructor.

.Default(string.Empty)

used only for generating the schema. This value will not be used for the value of this property for an empty string of your object.

Edit:



You can also use DynamicInsert()

in your mapping. Therefore, it will use the default database if you don't explain that value by value.

dynamic-insert (optional, defaults to false): Specifies that the INSERT SQL should be generated at runtime and contain only columns, the values ​​are not null.

+1


source


I also had problems setting an empty string as the default. Please note that I am using SQLite. After some investigation it seems that FluentNHibernate is generating a "wrong" SQL statement regarding fluent mapping.

Let's take

Map(m => m.Notes).Default(string.Empty).Not.Nullable();

      

or

Map(m => m.Notes).Default("").Not.Nullable();

      

from your example. We expect this SQL:

Notes TEXT DEFAULT "" NOT NULL

      

but it generates

Notes TEXT 

      

It is explicitly omitting "" as the default. Using



Map(m => m.Notes).Default(" ").Not.Nullable(); 

      

leads to

Notes TEXT DEFAULT   NOT NULL

      

Worse still, because its invalid SQL, because the "" character is missing.

Using

Map(m => m.Notes).Default("\"\"").Not.Nullable(); 

      

generates correct SQL like

Notes TEXT DEFAULT "" NOT NULL

      

actually uses an empty string as the default.

+3


source







All Articles