Skip to content

Adding Trim option in Reverse Engineer tool #300

Closed

Description

@Intecpsp

Can an option be added to append the following to all columns generated by using the Reverse Engineer tool to trim all leading and trailing spaces?

.HasConversion(v => v.Trim(), v => v.Trim())

Activity

  1. Intecpsp commented on Nov 7, 2019

    @Intecpsp
    Author

    I modified my context to add this to all columns, but then had them removed when a new table was added through the tool.

  2. jespersh commented on Nov 7, 2019

    @jespersh
    Contributor

    May I offer an alternative?
    Create a new partial class with your DbContext class and add this into it:

    public override int SaveChanges(bool acceptAllChangesOnSuccess)
    {
    	UpdateEntityValues();
    	return base.SaveChanges(acceptAllChangesOnSuccess);
    }
    
    public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
    {
    	UpdateEntityValues();
    	return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
    }
    
    public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
    	UpdateEntityValues();
    	return base.SaveChangesAsync(cancellationToken);
    }
    
    private void UpdateEntityValues()
    {
    	foreach (var entity in ChangeTracker.Entries())
    	{
    		foreach (var property in entity.Properties)
    		{
    			if (property.IsModified && property.CurrentValue.GetType() == typeof(string))
    			{
    				// Trim happens here
    				property.CurrentValue = property.CurrentValue.Trim(); 
    			}
    		}
    	}
    }
  3. Intecpsp commented on Nov 7, 2019

    @Intecpsp
    Author

    That works for mutations, but not querying. I'm using this to generate a model on a vendor database and they have the columns set to char instead of varchar.

  4. ErikEJ commented on Nov 7, 2019

    @ErikEJ
    Owner

    Just add a partial DbContext class, and implement the partial OnModelCreatingPartial method.

    Maybe I should add this to the docs?

    using Microsoft.EntityFrameworkCore;
    
    namespace MyApp.Domain.Models
    {
        public partial class OrganizationsContext
        {
            partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Organization>(entity =>
                {
                    entity
                    .Property(e => e.Name)
                    .HasConversion(v => v.Trim(), v => v.Trim());
                });
            }
        }
    }
    
    
  5. Intecpsp commented on Nov 7, 2019

    @Intecpsp
    Author

    I like that approach! Doing it that way allows me to code the relationships that were missed to! Thanks!

  6. ErikEJ commented on Nov 10, 2019

    @ErikEJ
    Owner

    Docs updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions