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

c# entity-framework-core sql-server

Question

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?

1
5
12/27/2018 3:21:06 PM

Accepted Answer

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!

0
12/31/2018 1:13:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow