Search code examples
databaseentity-frameworkwhitespacepaddingtrim

Whitespace when retrieving database row from Entity Framework


I am using Entity Framework and I am adding a User (UserName and Password) to a database table. This table has a UserName column (nchar(20)) and a Password column which is hashed (varchar(50)).

The problem occurs when I retrieve a User from the database. The username returned is padded with whitespace at the end (until 20 characters have been filled). I have solved this temporarily using the Trim() method however this seems impractical considering I need to do this on every column returned from the database.

What is the other solution?

Thanks!


Solution

  • You have to convert your column to nvarchar if you want your column to avoid the extra spaces. I personally don't like to use char(n) columns for values that aren't of fixed length. Otherwise you have to continue trimming