简体   繁体   中英

Entity Framework 6: Trim does not work properly

I have a process which should copy data from one database entity to another. Therefore I use Entity Framework 6 and LINQ expressions in C# :

public partial class ReferenceEntities : DbContext {
     public Nullable<System.DateTime> ActivationDate { get; set; }

     public string Code { get; set; }
}

this.ReferenceEntities.ReferenceEntity.AsEnumerable().Select(referenceEntry => new StagingReferenceLoader() {
    ActivationDate = referenceEntry.ActivationDate,
    Code = referenceEntry.Code?.TrimStart('0').Trim()
});

this.ProcessingEntities.StagingReferenceLoader.AddRange(stagingEntries);
this.ProcessingEntities.SaveChanges();

Code is char(11) column in both entities which are affected by this process.

Example:

In StagingReferenceLoader the Code value looks like this: 00000253089

With the Trim operations I want to make it look like this: 253089 . This looks fine on the first way, when I have executed it. But if I ran this SQL query on the database directly it gives my the result as following:

SELECT DATALENGTH(code), len(code), code
FROM staging.ReferenceLoader

datalength | len | code
11         | 6   | 253177     
11         | 6   | 270724     

Why does Entity Framework just does not do the trim operation?

The type of Code field must be varchar(11), because char(11) has a fixed length. The DB so will always fill it to the full length. Please, change it to varchar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM