¿Por qué Fluent NHibernate crea SIEMPRE un campo de texto a partir de una propiedad de cadena C # como DEFAULT?

I run this sample here: https://github.com/jagregory/fluent-nhibernate/blob/master/src/Examples.FirstProject/Program.cs

All C# Properties of type String are mapped into TEXT sql fields and not

nvarchar OR varchar as I would except it to be as DEFAULT setting.

¿Cómo puedo cambiar eso?

Sé que puedo hacerlo:

Map(x => x.Name).CustomSqlType("nvarchar").Length(50); // does not work !!!
Map(x => x.Name).CustomSqlType("nvarchar (50)"); // does work !!!

but I do not want to change every field...

UPDATE: ... therefore I can do a custom convention via =>

What I have to do is write a custom Convention class like:

public class ColumnStringToNVarCharConvention : IPropertyConvention, IPropertyConventionAcceptance
        public void Apply(IPropertyInstance instance)

        public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
            criteria.Expect(x => x.Property.GetType().Equals(typeof(string)));

Can someone proper the above code? I tested it and it did not work whyever...

preguntado el 08 de enero de 11 a las 18:01

Wouldn't it be more confusing if it no predeterminado a Texto NVARCHAR(MAX)? After all, any length chosen as a default would be just as arbitrary and your question could then be "Why does NH choose length x as the default for NVARCHAR fields?" -

The major part > 90 % of database string fields are varchar or nvarchar not TEXT which can not be indexed. So why not offer then varchar/nvarchar as DEFAULT? -

I have to manually edit my field settings from TEXT to Varchar because 10 % of the fields in average are used as TEXT ? Does not make sense. Why not make DEFAULT what is 90% ? Logical eh? -

Funny... this guy has the OPPOSITE problem just 6 months ago: stackoverflow.com/questions/3105391/… He gets as DEFAULT NVARCHAR and wants Text lol ??? -

Are you using the same dialect in your configuration as your database? -

2 Respuestas

You're using SQLite, correct? That's where your problem lies. It's described better in this answer to another post, but SQLite doesn't have data-types in the same sense as other databases. It's not a Fluent NHibernate issue, it's the way NHibernate deals with an almost data-typeless database.

For more strict databases, Fluent NHibernate defaults to more sensible data-types. It varies by database, but it will be an nvarchar para cuerdas.

contestado el 23 de mayo de 17 a las 14:05

I have read your post James about the sqlite datatypes. I knew a date is a string but I did not knew at all that there are only 5 datatypes using an sqlite IDe for years displaying stuff like varchar,nvarchar HAHA - Elisabeth

So the only reason why my IDE shows nvarchar,varchar etc.. is a friendly way of the IDE for people migrating from T-SQL,MySQL to sqlite because they are used to this datatypes. James would you though change my ColumnStringToNVarCharConvention class, please? - Elisabeth

You might try checking what compatibility level the database you are connecting to is. If it is set too low (80), it might be defaulting to text because NTEXT was not deprecated until SQL2005, which is compatibility level 90. This is not a guarantee, just a possible thought.

This is the MSDN instructions for checking to compatibility level...

To view or change the compatibility level of a database

After connecting to the appropriate instance of the SQL Server Database Engine, in Object Explorer, click the server name to expand the server tree.

Expand Databases, and, depending on the database, either select a user database or expand System Databases and select a system database.

Right-click the database, and then click Properties.

The Database Properties dialog box opens.

In the Select a page pane, click Options.

The current compatibility level is displayed in the Compatibility level list box.

To change the compatibility level, select a different option from the list. The choices are SQL Server 2000 (80), SQL Server 2005 (90), or SQL Server 2008 (100).

With SQLite, there are only 5 datatypes, one being string.


Every value in SQLite has one of five fundamental datatypes:

64-bit signed integer
64-bit IEEE floating point number

These constants are codes for each of those types.

Note that the SQLITE_TEXT constant was also used in SQLite version 2 for a completely different meaning. Software that links against both SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not SQLITE_TEXT.

Respondido el 09 de enero de 11 a las 00:01

Added more info to my above answer. You might also add a SQLite tag to the original question so you get someone who knows more about it to weigh in. - Jesse McCulloch

I retagged with fluent nhibernate because its a fluent problem not database related I am sure. I have updated my question again! - Elisabeth

It's not a problem with fluent, it's how NHibernate handles SQLite. - James Gregory

@James ok you beat me to it ;-) - Elisabeth

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.