Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot write unicode (emoji) into SQL Server table via context (Entity Framework Core)

I have a table in my SQL Server database with a column ReviewText of datatype NVARCHAR(MAX).

If I insert or update rows via a SQL query with N'' prefix, for example:

UPDATE [dbo].[Reviews] 
SET ReviewText = N'It's OK. 😊' 
WHERE Id = [id]

it works great and it will write smiley 😊 into the table.

But if I insert it from code:

var review = _context.UserReview.FirstOrDefault(x => x.Id == [id]);
review.ReviewText = "It's OK. 😊";

the code will store It's OK. ?? without smiley into the column.

How to fix this in code?

like image 444
Hennadii Feshchuk Avatar asked Dec 27 '18 15:12

Hennadii Feshchuk


1 Answers

I fixed this issue. The problem was in entity model mapping. I have changed

entity.Property(e => e.ReviewText).HasColumnType("text");

to

entity.Property(e => e.ReviewText).HasColumnType("nvarchar(max)"); 

Thanks to all!

like image 64
Hennadii Feshchuk Avatar answered Sep 29 '22 02:09

Hennadii Feshchuk