1

I have a column in a table that is :

QuestionDescription nchar(150)

The name of the table is Questions. When I put in the entity Questions an empty string, meaning:

Questions questionObj = new Questions();
questionObj.QuestionDescription = string.Empty();

Entity Framework saves a string of 150 whitespaces in the database.

Why?

I tried to put an empty string, but it always puts 150 whitespaces.

Any idea why?

Thanks

2
  • 2
    It's not Entity Framework that's to blame - this is SQL Server that's causing the storing of "whitespace" (since you're basically using the wrong datatype). The same effect would be observable if you used straight raw ADO.NET - this has nothing to do with EF .... Commented Apr 12, 2015 at 13:33
  • 1
    Note: White space characters is a category of characters, including spaces, tabs and line breaks. What you are talking about is the space character. Commented Apr 12, 2015 at 13:38

3 Answers 3

5

That's because the type is nchar instead of nvarchar. With nchar it will always be a value with the exact number of characters you specify, so it will be space filled by the DB, not EF. If you want an actually empty string in your DB then you should use nvarchar instead which will save string values up to the size you give, without padding them to the given size.

Sign up to request clarification or add additional context in comments.

Comments

4

This is a problem with your database. nchar is fixed length. Try nvarchar data type instead

Comments

1

I suggest u to use nvarchar data type instead nchar is fixed length, nvarchar is a varying length. Typically I would recommend you use nvarchar all the time, unless you have a short (< 5 char) field and you are 100% sure it is a set size.

The "n" at the beginning signifies a double byte character to store Unicode data. Use that if you need to handle multiple languages, or languages with double byte characters like Chinese.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.