You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Proposal: Specify columns to ignore during reverse engineering #572
when scaffolding from database, I often need to map a property to an enum type and not to its database column type. I know I can use handlebars template, but I have another suggestion which could make life easier. Would it be possible to expand the renaming file (or create a new one) to specify columns to ignore during import? This would allow me to use partial classes to define properties for those columns and partial method in OnModelCreating to define their mapping. I know such task would not be high in the priority list, so I would be more than happy to contribute.
//EFPT generated code
public partial class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
//my custom code
public partial class Person
{
public Gender Gender { get; set; }
}
public enum Gender
{
Male,
Female,
Other
}
public partial class MyDbContext
{
void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>(entity =>
{
entity.Property(e => e.Gender)
.whatevermapping();
});
}
}
That's the file I was looking at. I'm glad we have same idea.
I cloned the repository and will start working on a PR as soon as I can. The only problem is that I never created a VS Extension before so it will take some time before everything is setup.
@sm15455 As long as you have the Extensibility workload installed in Visual Studio, set EFCorePowerTools project as startup and set build platform to x86, it should be as simple a pressing F5!
Please feel free to discuss any design concerns before getting in too deep.
Modified the TableInformationModel class adding the ExcludedColumns (string[]) property.
Inherited from RelationalScaffoldingModelFactory and overriden the VisitTable method to remove excluded columns before the visitor creates the entity builder for the table.
Added the new scaffolder to the service provider builder.
Now I need to work on the UI side. I should:
Serialize and deserialize the ExcludedColumns property as a property of a table in the eft.config.json.
create a hierarchical view of the tables where user can view all tables and their columns. All columns for a table should be selected and the user must select those he wants to exclude.
Remember the user selection when UI is reloaded (it happens now for table selection and must do the same for columns)
The problem is that TableInformationModel is directly used in the ITableInformationViewModel model so I can't use the ExcludedColumns property because in the UI it's not a simple list of strings, but a list of (string, bool) (needed for selection). The most conservative approach I can think of is to add a ExcludedColumns property in the ITableInformationViewModel and then write code to map it to and from ExcludedColumns property in the TableInformationModel. Any thought on this?
My apologies.. I'm not following. Should this be done on the DB side or in the app itself?
We recently updated our API to .NET & EF 6.0. Within our scaffolded DbContext file, the temporal tables now get included (when they didn't before) on our model builder.
We have a context extension class that implements this method:
Activity
ErikEJ commentedon Nov 11, 2020
Please give an example of your modifications if a column was not included.
sm15455 commentedon Nov 11, 2020
ErikEJ commentedon Nov 12, 2020
Had a closer look at this, looks like an approach to try would be to implement https://github.com/dotnet/efcore/blob/main/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs#L299 and register this override when building the service provider, if a list of columns to exclude are available, then modify the DatabaseTable model by removing the excluded columns.
we would need a way to pass the list of columns to exclude to this class.
The list of columns to exclude could be under each Table in the tables list in efpt.config.json.
PRs accepted.
sm15455 commentedon Nov 12, 2020
That's the file I was looking at. I'm glad we have same idea.
I cloned the repository and will start working on a PR as soon as I can. The only problem is that I never created a VS Extension before so it will take some time before everything is setup.
ErikEJ commentedon Nov 12, 2020
@sm15455 As long as you have the Extensibility workload installed in Visual Studio, set EFCorePowerTools project as startup and set build platform to x86, it should be as simple a pressing F5!
Please feel free to discuss any design concerns before getting in too deep.
ErikEJ commentedon Nov 12, 2020
For testing just scaffolding, make the efreveng project your startup project!
ErikEJ commentedon Nov 12, 2020
Duplicate of #353
paul1923 commentedon Nov 13, 2020
Another use case when ignoring column names is helpful is Temporal Tables, ie: [SysEndTime], [SysStartTime] columns
ErikEJ commentedon Nov 13, 2020
@paul1923 Sure, but you can also mark them hidden.
sm15455 commentedon Nov 13, 2020
@ErikEJ I'm slowly moving on.
On The reveng side I did the following changes
Now I need to work on the UI side. I should:
The problem is that TableInformationModel is directly used in the ITableInformationViewModel model so I can't use the ExcludedColumns property because in the UI it's not a simple list of strings, but a list of (string, bool) (needed for selection). The most conservative approach I can think of is to add a ExcludedColumns property in the ITableInformationViewModel and then write code to map it to and from ExcludedColumns property in the TableInformationModel. Any thought on this?
ErikEJ commentedon Nov 13, 2020
Fantastic!
I suggest that we make it a manual feature for now, so folks can try it out, just make sure that we serialize and deserialize properly.
Then we can also think about improving the View Model, possibly map to a class that is better suited for UI display than the TableInformationModel.
ErikEJ commentedon Nov 13, 2020
Submitting a PR with that will also make the changes and code review much more manageable.
5 remaining items
ErikEJ commentedon Nov 14, 2020
No worries, been there myself - let me know if you are unblocked.
paul1923 commentedon Nov 15, 2020
@ErikEJ
Thanks for the tip on HIDDEN columns. I've opted instead to create another DbContext partial class for the fields I want ignored.
sm15455 commentedon Nov 16, 2020
@ErikEJ I close this issue as the feature is now implemented. I'll open a new one for the UI
ErikEJ commentedon Nov 16, 2020
It is not in the daily build yet, hang in there...
Implement excluded columns
Implement excluded columns (#586)
ErikEJ commentedon Nov 17, 2020
@sm15455
Some thoughts on next step:
Create new class to exchange the table list (stop using TableInformation).
Add columns to this class.
Add columns to the output from TableList command if EFPT CLI tool.
Extend TableInformation with a new, non-serialized List, where ColumnInformation has:
string Name
bool Excluded
Construct the ExcludeColumns property when loading options, and set when saving options.
Bind ColumnInfomation to UI - no idea how...
ErikEJ commentedon Aug 19, 2022
Custom enum mapping is now available with EF Core 7 and T4 templates
Colemartindale commentedon Feb 15, 2023
How do you mark Temporal tables as hidden?
ErikEJ commentedon Feb 15, 2023
@Colemartindale exclude them??
Colemartindale commentedon Feb 15, 2023
My apologies.. I'm not following. Should this be done on the DB side or in the app itself?
We recently updated our API to .NET & EF 6.0. Within our scaffolded DbContext file, the temporal tables now get included (when they didn't before) on our model builder.
We have a context extension class that implements this method:
partial void OnModelCreatingPartial(ModelBuilder modelBuilder) { modelBuilder.Entity<AnalysisMetric>(entity => {entity.Ignore("analysis_metric_HISTORY"); }); }but this doesn't seem to work
ErikEJ commentedon Feb 15, 2023
Please create a new issue and share a repro script and I will have a look.