Cannot insert Cyrillic characters correctly in NTEXT field in SQL Server database

How to insert cyrillic characters into my database?

I have a table Articlesand web API controller with CRUD operations.

Articles { id: int, summary: nvarchar, text: ntext }

If I insert data using t-sql like

insert into Articles values (1, N'', N'  ')

everything works fine - the data in the database is fine - all Russian characters look good.

But if I insert data using the ajax 'text' field, filled with question marks: '??????????????? ''

$.post("http://localhost:1000/api/articles", 
  {
    id: 1,
    summary: "",
    text: "  "
  }
).always(function(res) {console.log(res)} );

Where is my mistake?

PS: connectionString, if necessary:

<add name="DefaultConnection" connectionString="Data Source=xxx;Initial Catalog=yyy;User ID=zzz;Password=***;" providerName="System.Data.SqlClient"/>

PPS: I use the automatic POST deafult action from WebAPI:

// POST: api/Articles
[ResponseType(typeof(Article))]
public IHttpActionResult PostArticle(Article article)
{
  if (!ModelState.IsValid)
  {
    return BadRequest(ModelState);
  }

  db.Articles.Add(article);
  db.SaveChanges();

  return CreatedAtRoute("DefaultApi", new { id = article.ArticleId }, article);
}

UPD:

[DataContract]
public class Article
{
  [DataMember]
  public int Id { get; set; }

  [DataMember]
  public string Summary { get; set; }

  [DataMember]
  [Column(TypeName = "ntext")]
  public string Text { get; set; }
}
0
source share
1 answer

, , NTEXT EF Code First: , Unicode. Fluent API, , OnModelCreating() :

modelBuilder
  .Entity<Aticle>()         // Choose the entity to configure
  .Property(c=> c.Text)     // Choose the property to configure
  .HasColumnType("NTEXT")   // Specify the DB type
  .IsUnicode(true);         // Specify Unicode (for EF internally generated params)

, NTEXT. ntext, text image (Transact-SQL) docs:

ntext, Microsoft SQL Server. , . nvarchar (max), varchar (max) varbinary (max).

+1

Source: https://habr.com/ru/post/1622999/


All Articles