Search code examples
c#entity-frameworkpdfef-database-firstcyrillic

How can I insert ntext with Entity Framework?


I need execute this type of command `Insert table(column) values (N'текст') with Entity Framework

Here my code

 public ActionResult PDFGeneration(string format)
 {
        var myFont = Path.Combine(Server.MapPath("~/App_Data/Font/"), "font.ttf");
        Rectangle pgeSize = new Rectangle(595, 792);

        Document pdfDoc = new Document(PageSize.A4, 50, 50, 50, 0f);
        PdfWriter pdfwriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

        pdfDoc.Open();

        BaseFont bfR = BaseFont.CreateFont(myFont ,BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        BaseColor clrBlack = new BaseColor(0, 0, 0);
        Font fntHead = new Font(bfR, 12, Font.NORMAL, clrBlack);

        //pdfDoc.Add(new Paragraph("Літера ї є я ь", fntHead));


        using (MedicalDictionaryEntities mde = new MedicalDictionaryEntities())
        {

            mde.Database.Connection.Open();
            var listTUa = from x in mde.TranslationUA select x;
            foreach (TranslationUA tua in listTUa)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append(tua.Word).Append(" - ");

                foreach(TranslationEN ten in tua.TranslationEN)
                {
                    builder.Append(ten.Word).Append(", ");
                }

                pdfDoc.Add(new Paragraph(builder.ToString(),fntHead));
            }
        }

        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Generated.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();

        return View("Index");
    }

I works but I am getting ??????? instead of the Cyrillic letters.

I added these columns to entity model, but it didn't help

    [MaxLength]
    [Column(TypeName = "ntext")]
    public string Word { get; set; }

I tried to execute Insert table(column) values (N'текст') in SQL Server and it worked.

So my question is: how can I execute this type of command using code and EF?


Solution

  • If you are using Database-First or Model-First, you should edit your edmx. Setting those attributes on model will not work for you.

    Open your edmx in designer and then select your field, in properties of the field, Set Max Length to Max and Unicode to true.

    You can also remove that entity from your edmx and then right click and select Update Model from Database ... and add the entity again, this way if the field is ntext in database, settings will be done for the field in edmx.