I’m developing an app where I need to insert names using special characters like áéíóúñ and I’m using linq to entities with Visual C#.
My database server is SQL 2008 express and the default collation of my database is SQL_Latin1_General_CP1251_CI_AS, however I changed the collation of the fields to SQL_Latin1_General_CP1_CI_AI where I need to insert the special characters mentioned above.
If I insert names with special characters using code similar to this:
Cliente cli = new Cliente()
{
Nombres = cliente.nombres,
ApellidoP = cliente.apellidoP,
ApellidoM = cliente.apellidoM,
FechaNac = cliente.fechaNacimiento
};
context.clientes.AddObject(cli);
context.SaveChanges();
The special characters are translated to their equivalent, but if I insert names with special characters directly in the database (management studio) I have no problems inserting those characters.
I changed fields collation to SQL_Latin1_General_CP1_CI_AI after creating my project in visual studio, so I thought updating model from database would solve the problem but I was wrong.
I also used the debugger to check if special characters were present when I create a new instance of Cliente object, and yes, they’re there.
What could be the problem?
I do not see anything wrong with your code or approach. This seems to be an issue with the Database. The best soltion would be to change the BD collation to SQL_Latin1_General_CP1_CI_AI, but that is usually hard to do if you are pass the development phase.
You could try one of the following:
Eliminate the entity or entities that have the field properties that hold the special characters from your model, run Update Model from Database again and re-add the tables. Sometimes an update is not enough to refresh the entities, and maybe your field specific collation configuratino was not taken into account the first time.
Take a look at this post. Maybe the problem has to do with the design of your tables.
Or this other post. Using a similar approach, maybe it is possible to change the collation of your BD (if it is not being used by other applications – not likely).
Hope this helps.