Search code examples
c#sql-serverasp.net-mvcentity-frameworkcyrillic

Result of adding data by EntityFramework in Cyrillic is exclamation marks. SQL Server


I have an web application where I just add some data by user inputs. However, when I test at real web server, I just see exclamation marks instead of "Привет".

using (DatabaseEntities context = new DatabaseEntities())
{
   name = "Человек";
   surname = "Привет";   
   Students student = new Students();
   apho.ID = ID;
   apho.Name = name;
   apho.Surname = surname;
   context.AddObject("Students", student);
   context.SaveChanges();                            
}

The result is for Name: "???????" and for Surname is: "??????"

Any help would be greatly appreciated!


Solution

  • @mfanto is right. I use incorrect collation. A correct procedure is:

    alter table Students
    alter column
    Surname Nvarchar(255)  COLLATE Cyrillic_General_CI_AS_KS not null
    go
    

    So my orders actions to see Russian letters in SQL Server through Entity Framework are:

    1. Create Unicode fields(NVARCHAR)
    2. Change collation(see above code(sql query)). Maybe it is not necessary as I've created Unicode fields.
    3. Create new ADO.NET Entity Data Model as collated columns from SQL table become in Entity Framework properties of classes are set to Unicode - true(I've seen these properties by clicking at properties in diagram)

    That's it!:)