Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework C# Insert Data russian encoding problems

I'm using an EDM model in my project.

When I insert russian words in the database via a post request I get ??????

Controller:

[Authorize]
[HttpPost]
public string DescEdit(FormCollection formValues)
{
    var CurrentUserPhoto = User.Identity.Name;
    string x = Request.Form["id"];
    Int64 id = Convert.ToInt64(x);
    photos upPhotoDesc = photosRepository.GetPhotosById(id, CurrentUserPhoto);
    upPhotoDesc.description = Request.Form["value"];
    photosRepository.Save();

    return Request.Form["value"];
}
  1. In the database all charset are set to utf-8
  2. In the layout page content enc type is utf-8

Database code:

CREATE TABLE `photos` (
  `id` bigint(255) NOT NULL AUTO_INCREMENT,
  `done` tinyint(1) NOT NULL DEFAULT '0',
  `imgsmall` varchar(255) NOT NULL DEFAULT '',
  `imgcrop` varchar(255) NOT NULL DEFAULT '',
  `imgmiddle` varchar(255) NOT NULL DEFAULT '',
  `imgbig` varchar(255) NOT NULL DEFAULT '',
  `full_size` varchar(255) NOT NULL DEFAULT '',
  `description` varchar(255) DEFAULT NULL,
  `name` varchar(255) DEFAULT NULL,
  `permission` tinyint(1) NOT NULL DEFAULT '0',
  `userid` int(11) NOT NULL DEFAULT '0',
  `userlogin` varchar(255) NOT NULL DEFAULT '',
  `rating` smallint(5) DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `indx_photos_1` (`id`,`userlogin`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;
like image 200
Evgeniy Labunskiy Avatar asked May 04 '11 20:05

Evgeniy Labunskiy


People also ask

What is use of Entity Framework in C#?

The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored.

Is Entity Framework still used?

Entity Framework 4.1, 4.2, 4.3, and 5.0 and all patches of these versions are out-of-support and should not be used. The NuGet packages for Entity Framework 4.1, 4.2, 4.3, and 5.0 are no longer supported.

What means Entity Framework?

Entity Framework (EF) is an object-relational mapper that enables . NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. Get it: Add this to your project and start taking advantage of this powerful O/RM.


1 Answers

Add charset=utf8 to the connection string of Entity Framework
Here is a working add node:

<add name="photostorageEntities"
     connectionString="metadata=res://*/Models.Photos.csdl|res://*/Models.Photos.ssdl|res://*/Models.Photos.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=ServerIP;User Id=UID;password=PASS;Persist Security Info=True;database=photostorage; Charset=utf8&quot;"
     providerName="System.Data.EntityClient" />
like image 71
Evgeniy Labunskiy Avatar answered Sep 19 '22 19:09

Evgeniy Labunskiy