Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top