How to trim all char fields (C# string) before writing to database for all tables and all add or updates in EF Core?

huangapple go评论74阅读模式
英文:

How to trim all char fields (C# string) before writing to database for all tables and all add or updates in EF Core?

问题

我希望 EF Core 在将数据写入数据库之前,自动将所有表(实体)和所有添加或更新操作的所有字符字段(C# 字符串)修剪。

英文:

I want EF Core to trim all char fields (C# strings) automatically before writing to the database for all tables (entities) and all add or update operations.

答案1

得分: 3

You can achieve this by overriding the SaveChanges method of your DbContext class. In the overridden method, you can iterate through all the entities in the ChangeTracker and trim all the string properties before saving the changes to the database.

** AppDbContext.cs **
public partial class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Company> Companies { get; set; }
    ... // other DbSets

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      ...
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

** AppDbContextPartial.cs **
public partial class AppDbContext
{
    public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        TrimStringProperties();
        return await base.SaveChangesAsync(cancellationToken);
    }

    private void TrimStringProperties()
    {
        var entries = ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified);

        foreach (var entry in entries)
        {
            foreach (var property in entry.Properties)
            {
                if (property.CurrentValue is string stringValue)
                {
                    property.CurrentValue = stringValue.Trim(); // here
                }
            }
        }
    }
}

How to trim all char fields (C# string) before writing to database for all tables and all add or updates in EF Core?

英文:

You can achieve this by overriding the SaveChanges method of your DbContext class. In the overridden method, you can iterate through all the entities in the ChangeTracker and trim all the string properties before saving the changes to the database.

** AppDbContext.cs **
public partial class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions&lt;AppDbContext&gt; options)
        : base(options)
    {
    }

    public virtual DbSet&lt;Company&gt; Companies { get; set; }
    ... // other DbSets

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      ...
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

** AppDbContextPartial.cs **
public partial class AppDbContext
{
    public override async Task&lt;int&gt; SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        TrimStringProperties();
        return await base.SaveChangesAsync(cancellationToken);
    }

    private void TrimStringProperties()
    {
        var entries = ChangeTracker.Entries()
            .Where(e =&gt; e.State == EntityState.Added || e.State == EntityState.Modified);

        foreach (var entry in entries)
        {
            foreach (var property in entry.Properties)
            {
                if (property.CurrentValue is string stringValue)
                {
                    property.CurrentValue = stringValue.Trim(); // here
                }
            }
        }
    }
}

How to trim all char fields (C# string) before writing to database for all tables and all add or updates in EF Core?

huangapple
  • 本文由 发表于 2023年7月6日 19:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定