简体   繁体   English

实体框架6:修剪无法正常工作

[英]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# : 因此我在C#中使用Entity Framework 6LINQ表达式:

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. Code是受此过程影响的两个实体中的char(11)列。

Example: 例:

In StagingReferenceLoader the Code value looks like this: 00000253089 StagingReferenceLoader中Code值如下所示: 00000253089

With the Trim operations I want to make it look like this: 253089 . 使用Trim操作,我想让它看起来像这样: 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: 但是如果我直接在数据库上运行这个SQL查询,它会得到如下结果:

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? 为什么Entity Framework不执行修剪操作?

The type of Code field must be varchar(11), because char(11) has a fixed length. Code字段的类型必须是varchar(11),因为char(11)具有固定长度。 The DB so will always fill it to the full length. DB因此将始终填充到全长。 Please, change it to varchar. 请将其更改为varchar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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