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

James Gregory
  • 14,173
  • 2
  • 42
  • 60
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • 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?" – Kent Boogaart Jan 08 '11 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? – Elisabeth Jan 08 '11 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? – Elisabeth Jan 08 '11 at 19:44
  • 1
    Funny... this guy has the OPPOSITE problem just 6 months ago: http://stackoverflow.com/questions/3105391/mapping-large-string-with-fluent-nhibernate He gets as DEFAULT NVARCHAR and wants Text lol ??? – Elisabeth Jan 08 '11 at 19:53
  • Are you using the same dialect in your configuration as your database? – Paco Jan 08 '11 at 20:21
  • @Kent CONVENTION over CONFIGURATION !!! :) – Elisabeth Jan 08 '11 at 20:23
  • @Paco same dialect? I use C# class with String property and get a Text field in my database. There is not more info. – Elisabeth Jan 08 '11 at 20:24
  • I have updated my question at top with a sample! – Elisabeth Jan 08 '11 at 20:30
  • With dialect means something like mysql, sqlserver2000, sqlserver2008 – Paco Jan 08 '11 at 20:34

2 Answers2

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.

Community
  • 1
  • 1
James Gregory
  • 14,173
  • 2
  • 42
  • 60
  • 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 Jan 09 '11 at 12:38
  • 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 Jan 09 '11 at 12:42
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.

Jesse McCulloch
  • 683
  • 4
  • 13