2

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.

How can I change that?

I know I can do this:

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)
        {
            instance.CustomSqlType("nvarchar");
        }

        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...

9
  • 1
    Wouldn't it be more confusing if it didn't default to Text, or 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?" Commented Jan 8, 2011 at 18:51
  • 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? Commented Jan 8, 2011 at 19:41
  • 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? Commented Jan 8, 2011 at 19:44
  • 1
    Funny... this guy has the OPPOSITE problem just 6 months ago: stackoverflow.com/questions/3105391/… He gets as DEFAULT NVARCHAR and wants Text lol ??? Commented Jan 8, 2011 at 19:53
  • Are you using the same dialect in your configuration as your database? Commented Jan 8, 2011 at 20:21

2 Answers 2

5

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 for strings.

Sign up to request clarification or add additional context in comments.

2 Comments

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
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?
0

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.

http://www.sqlite.org/c3ref/c_blob.html

Every value in SQLite has one of five fundamental datatypes:

64-bit signed integer
64-bit IEEE floating point number
string
BLOB
NULL

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.

4 Comments

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.
I retagged with fluent nhibernate because its a fluent problem not database related I am sure. I have updated my question again!
It's not a problem with fluent, it's how NHibernate handles SQLite.
@James ok you beat me to it ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.