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

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.

Incorrect string matching behavior on SQL Server with whitespace , nesterenko-kv added the type-bug label on Dec 25, 2019. @ajcvickers. Copy link #17598 is not currently scheduled for EF Core 5. On the other hand, we Rewrite string equality with LIKE on SQL Server #19881. Closed. SQL Server ignores trailing spaces in string values. Therefore, passing trailing spaces in the string can lead to unpredictable results, even failures. If you have to have trailing spaces in your string, you should consider appending a white space character at the end, so that SQL Server does not trim the string.

TomTom's answer is correct but if you were like me you may still be having an issue.

I changed my table column to varchar(10) but was still having the spacing issue. I found that my issue was also with my Entity Context definition. Make sure in your .modelBuilder.Entity<your_table>() does not have the property .IsFixedLength() on it. I took that out and that fixed my issue.

EF6.1–Workaround Trailing Blanks Issue in String Joins, The issue is that SQL Server ignores trailing spaces when comparing strings but the .NET string functions (which EF uses) does not. On SQL  Another and better solution would be to add an interception for Entity Framework that will trim all fields of certain data types. String Interceptor class. As you can see, this will trim fields of types nvarchar, varchar, char, and nchar. In the SQL generated, it will add a LTRIM and RTRIM to each field.

I have two extension methods that will trim nchar fields within entities. The first will trim a single entity, and the second uses the first to trim a list of entities.

public static T TrimSingleEntity<T>(this T entity)
        if (entity == null) return entity;
        var props = entity.GetType()
                .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                .Where(prop => prop.PropertyType == typeof(string))
                .Where(prop => prop.GetIndexParameters().Length == 0)
                .Where(prop => prop.CanWrite && prop.CanRead);
        foreach (var prop in props)
            var value = (string)prop.GetValue(entity, null);
            if (value == null) continue;
            value = value.Trim();
            prop.SetValue(entity, value, null);
        return entity;

public static List<T> TrimEntityList<T>(this List<T> entityList)
        foreach (var entity in entityList) TrimSingleEntity(entity);
        return entityList;

Entity string is padded with spaces for nvarchar?? - MSDN, The entity model framework is padding the FirstName value with spaces so the total character count = 50. The below test code fails because  I'm using Entity Framework (6-beta) in an ASP.NET MVC application. I created a database using the SQL Server Management Studio. I can connect to the DB using Management Studio and my local Windows user account. I dind't change the default configuration, so the SQL Server stores the data files in :

white spaces are being added to my database table, NET, Entity Framework, LINQ to SQL, NHibernate/white spaces are being added I am using the string to represent the imageurl property of images work because my guess was that sql server was adding the white to make  LTRIM is used to remove leading spaces from a string. RTRIM is used to remove trailing spaces from a string. Starting with SQL Server 2017, we now have the much awaited TRIM function which will remove both the leading and trailing spaces from the string. However that is out of scope of the current article.

Microsoft ADO.NET 4 Step by Step, You can build a model using the Entity Data Model Designer starting from a blank Slate, When you add a new Entity Data Model (EDM) to your project, Visual Studio adds EF-Specific metadata key-value pairs that help the Framework access In this sample, the expected SQL Server connection string comes after three  Browse other questions tagged c# sql-server entity-framework connection-string remote-server or ask your own question. The Overflow Blog Learning to work asynchronously takes time

Practical Database Programming with Visual Basic.NET, example of creating a new data table object and adding columns and rows into this table. a new instance of the data table FacultyTable is created and initialized to a blank table. NET Entity Framework 4.1 Most traditional databases use the relational model of data, such as Microsoft Access, SQL Server, and Oracle. Managing Connection Strings in Entity Framework Core Connection strings contain information about the data source that is being connected to. This information varies from provider to provider, but will usually include the name and location of the source, and optionally some means of authenticating the user.

  • I spent hours trying to figure out the reason for this. Is there a reason why you'd ever want to fill in the rest of the characters with blanks?