Unable to use Turkish characters with Entity Framework

We have a large ASP.NET 4.0 SaaS application used internationally. We are slowly migrating our classic data providers to the Entity Framework.

We are using MS Sql Server 2008. For Turkish clients we use Turkish collation "Turkish_CI_AS" for character columns. That is, 8-bit varchar fields (we are not using 16-bit nvarchar columns)

Now I am facing a problem when I add new objects with EF. Changed special characters like "ş":

    using (TestEntities myEntity = new TestEntities())
    {
                    MyObject test = new MyObject()
                    {
                        TestString = "baş"
                    };

                    myEntity.MyObjects.AddObject(test);
                    myEntity.SaveChanges();
    }

      

When I go to the debugger, "test.TestString" is still "baş" in this line of code:

myEntity.MyObjects.AddObject(test);

      

However, in the database, the "TestString" field is "bas". "Ş" is saved as "s". It doesn't happen with my old dataprovider methods.

How do I add Turkish characters to my database using EF? Anyone have any good suggestions? I've tried some things but can't figure it out :)

tnx, Frank

change

running the profiler on a small test setup shows this:

exec sp_executesql N'insert [dbo].[TestObjects]([TestChar])
values (@0)
select [TestId]
from [dbo].[TestObjects]
where @@ROWCOUNT > 0 and [TestId] = scope_identity()',N'@0 varchar(255)',@0='Bas'

      

obviously sql is getting the wrong value, it is indeed .NET that is responsible.

+3


source to share


1 answer


Ok I figured it out. In MySql it is possible to add encoding information to the connection string, Microsoft SQL does not allow this.

EF seems to define the encoding by considering the default database collation. In our setup, the database mapping was Latin1 and the customer specific tables have the mapping "Turkish_CI_AS".

So, I have two options:

1) change the default setting for the entire database (impact on system and configuration tables, etc.)



2) change my columns to nvarchar

The transition to unicode will be complete at the end while we use some good old ADO.NET that just works! :)

tnx for reflection with me

+1


source







All Articles