Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Trim option in Reverse Engineer tool #300

Closed
Intecpsp opened this issue Nov 7, 2019 · 6 comments
Closed

Adding Trim option in Reverse Engineer tool #300

Intecpsp opened this issue Nov 7, 2019 · 6 comments

Comments

@Intecpsp
Copy link

Intecpsp commented Nov 7, 2019

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())
@Intecpsp
Copy link
Author

Intecpsp commented Nov 7, 2019

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

@jespersh
Copy link
Contributor

jespersh commented Nov 7, 2019

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(); 
			}
		}
	}
}

@Intecpsp
Copy link
Author

Intecpsp commented Nov 7, 2019

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.

@ErikEJ
Copy link
Owner

ErikEJ commented Nov 7, 2019

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());
            });
        }
    }
}

@Intecpsp
Copy link
Author

Intecpsp commented Nov 7, 2019

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

@ErikEJ
Copy link
Owner

ErikEJ commented Nov 10, 2019

Docs updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants