Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 957
francisg3 Avatar asked Jul 20 '26 18:07

francisg3


1 Answers

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

like image 59
TGH Avatar answered Jul 22 '26 11:07

TGH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!