-
-
Notifications
You must be signed in to change notification settings - Fork 301
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
Easy column overrides #1472
Comments
Any good suggestions for how to implement this? |
Sure Erik, Sorry for being so blunt with the 1 star review, but I genuinely feel this tools is missing too much features to be a Power Tool. It is my personal opinion, but please don't take it personally! I can only admire the work you put into this. Now to my suggestion: Back in the old days, just before Microsoft decided the Entity Framework had to be replaced bij the EF Core (which was missing essential features like group by, geometry and lazy loading and took years to reach the former functionality) I was using T4 with a .tt file. This file was very similar to the efpt.config.json of the tools. But it had more features, it had actual C# code, for example:
Or for table renaming:
In short, if there would be overridable functions for the steps in the scaffolding this would make it really flexible, powerful! |
Thanks for the suggestion - this will be possible soon with EF Core 7 due to support for (wait for it) .... T4 templates! Renaming is currently possible using individual renames or a regex. I would be very grateful if you would consider updating your review - having good reviews is important for me for many reasons. |
That's great news! Did not know that. I suppose you are going to support this? |
Of course I will. Will you consider updating your review? |
Thanks for the review update. I am closing this for now, and will confirm this a part of the EF Core 7 support |
@wijnsema I implemented this in the latest daily build, would be grateful if you could try it out. |
Nice work Erik! I tried it with a test project and it seems to be working OK. But it turns out I need to learn T4 first... Not that obvious to achieve what I want (enum columns). Did you create the included T4 yourself? One other problem for me is that I need to step up to .NET7 preview, but my product is running .NET6 in production. Anyway, great start. |
Yeah, there are some hurdles including tooling and learning curve. These are the default templates provided by the EF Core team. |
I will try to come up with a sample |
To replace int values with enum types you can modify EntityType.t4 like this: Add on line 22: var enumList = new List<(string EntityTypeName, string PropertyName, string? EnumType)>();
// add your enums here:
//enumList.Add( ("Customer", "Rating", "Rating") );
//enumList.Add( ("Entity", "Property", "EnumType?") ) Then on line 107 add/update as follows: var enumTuple = enumList.SingleOrDefault(t => t.EntityTypeName == EntityType.Name && t.PropertyName == property.Name);
var needsInitializer = Options.UseNullableReferenceTypes && !property.IsNullable && !property.ClrType.IsValueType && enumTuple.EnumType == null;
var propertyType = enumTuple.EnumType ?? code.Reference(property.ClrType);
#>
public <#= propertyType #><#= needsNullable ? "?" : "" #> <#= property.Name #> { get; set; }<#= needsInitializer ? " = null!;" : "" #> |
Wow Eric! I tried this out and it works really well! Thanks quick replies! This T4 is a great contribution, for me and for everyone I think. |
You are welcome, glad you like it. |
@ErikEJ I was able to fix it like this, but I'm not sure if that's the best/right way to do it: usings.AddRange(code.GetRequiredUsings(property.ClrType));
var enumTuple = enumList.SingleOrDefault(t => t.EntityTypeName == EntityType.Name && t.PropertyName == property.Name);
var needsNullable = Options.UseNullableReferenceTypes && property.IsNullable && (enumTuple.EnumType != null || !property.ClrType.IsValueType);
var needsInitializer = Options.UseNullableReferenceTypes && !property.IsNullable && !property.ClrType.IsValueType;
var propertyType = enumTuple.EnumType ?? code.Reference(property.ClrType);
#>
public <#= propertyType #><#= needsNullable ? "?" : "" #> <#= property.Name #> { get; set; }<#= needsInitializer ? " = null!;" : "" #>
<# |
@MarvinNorway Looks fine, but why have a nullable enum in the first place? |
Because the database column is nullable...? |
@MarvinNorway Yeah, I get that. Thanks for the update to the sample |
Thanks @ErikEJ for your sample and @MarvinNorway for your addition. One extra point:
it should be
Or you could just do |
Erik, I like the direction you are going, I use enums a lot as they create strongly typed items so, I get fewer runtime errors when values change (I get compile time errors :-) ). I did some stuff with an older tool and went to the trouble of building the t4 items to get what I needed. I wanted to drive the enums from the DB. As far as nulls go, I made it a standard to have a zero value (integer driven) item in every enum and it was always defined as 'Undefined' and it is the default value if null comes from the db. I am working against a product that has a lot of DB stuff in it and, I can't control it, but I can read it. Many settings are set by values, and I created some routines that create enums from those settings along with a default set of utilities that get spooled out when the code is generated (Value, Description etc.). I also use 'User Defined Types' with extended properties (Yes, I know not exactly what they were designed for but seem to work well) to create enums for my settings with a similar set of utilities with each generation. I am attaching a sample, if any of it is useful feel free to use etc. |
For anyone wondering: This does not work with Npgsql.EntityFrameworkCore.PostgreSQL |
It should be easy to override column scaffolding, for example for
enum
columns.Instead it's such a hassle that I gave up. There are some hints in issue 572, but this is not enough information for a 'user'.
Without this feature this tools has no real value over the Microsoft tools, at least not for me.
The text was updated successfully, but these errors were encountered: