Skip to content
Advertisement

Entity Framework and SQL Server adds blanks in strings?

I am building a ASP.NET MVC application with Entity Framework and SQL Server.

I have noticed that when I read back SQL Server columns of type nchar or text there will be added blanks at the end of the string. To fix this I have to run Trim() on the string?

How can I solve this?

Best Regards

Advertisement

Answer

I have notice that when I read back MS
SQL columns of type nchar

NCHAR is fixed length. If an INSERTed or UPDATEed value has a smaller length, then spaces are added. Naturally you read back with additional blanks.

This happens when the database is set up in such a way to add blanks.

if you do not want fixed length strings, don’t use a fixed length field type. Instead of NCHAR use NVARCHAR. The VAR in there indicates variable length.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement