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

Adds support for Using EntityTypeConfiguration files. #2582

Merged
merged 7 commits into from
Nov 8, 2024

Conversation

jwyza-pi
Copy link
Contributor

@jwyza-pi jwyza-pi commented Oct 16, 2024

Handles most of #2583. (won't be handling the GUI side of things)

TODO:

  • - Add Code to Ensure the scaffolded configuration files are put into the Configurations folder.
  • - Pass the rootNameSpace into the scaffolding.
  • - Customize the DbContext.t4 to support EntityTypeConfiguration.t4
  • - Add a new config option: use-t4-split to CLI
  • - Warn if using both use-t4 and use-t4-split. In the case of using both, respect use-t4 instead.
  • - Only use the rootNameSpace value if use-t4-split is used.
  • - Add new zip files for .net 8 that are similar to T4_800.zip, named T4_800_Split.zip that contains the EntityTypeConfiguration.t4 and the modified DbContext.t4 (and the EntityType.t4)
    • - Add the functionality to unzip this file into CodeTemplates in the event that they don't exist.

Couple items on this approach:

  1. I grabbed the EntityTypeConfiguration from https://github.com/R4ND3LL/EntityFrameworkRuler/blob/main/src/EntityFrameworkRuler.Design/Resources/EntityTypeConfiguration.t4 (which was linked in T4 customizations and tips #1499. It's for .Net7, but for whatever reason, MS didn't include it in their ef templates for .net 8 :(.

  2. You need to have context-splitting-preview turned off.

  3. One note is that to make this work you need to have a modified version of DbContext.t4 that replaces the ModelLines with calling the ApplyConfiguration instead. Not sure how we want to represent this if someone wants to use this. This is now covered by having the version of the DbContext.t4 in the T4_800_Split.zip file.

So before: (leaving the info below just for future reference as to what is different)

     StringBuilder mainEnvironment;
    foreach (var entityType in Model.GetEntityTypes().Where(e => !e.IsSimpleManyToManyJoinEntityType()))
    {
        // Save all previously generated code, and start generating into a new temporary environment
        mainEnvironment = GenerationEnvironment;
        GenerationEnvironment = new StringBuilder();

        if (anyConfiguration)
        {
            WriteLine("");
        }

        var anyEntityTypeConfiguration = false;
#>

After:

    foreach (var entityType in Model.GetEntityTypes().Where(e => !e.IsSimpleManyToManyJoinEntityType()))
    {
        // Save all previously generated code, and start generating into a new temporary environment
        mainEnvironment = GenerationEnvironment;
        GenerationEnvironment = new StringBuilder();

        if (anyConfiguration)
        {
            WriteLine("");
        }
        
        #>
        modelBuilder.ApplyConfiguration(new Configurations.<#= entityType.Name #>Configuration());
<#
        anyConfiguration = true;
        mainEnvironment.Append(GenerationEnvironment);
        // Resume generating code into the main environment
        GenerationEnvironment = mainEnvironment;
        continue;

        var anyEntityTypeConfiguration = false;
#>

@jwyza-pi jwyza-pi marked this pull request as ready for review October 16, 2024 16:44
@jwyza-pi jwyza-pi changed the title Adds support for Using EntityTypeConfiguration files. WIP: Adds support for Using EntityTypeConfiguration files. Oct 17, 2024
@jwyza-pi jwyza-pi marked this pull request as draft October 17, 2024 13:44
…TypeConfiguraiton usage. adds warning if you try to use use-t4 and use-t4-split. Adds warning if you try to use use-t4 and split-dbcontext-preview.
@jwyza-pi
Copy link
Contributor Author

jwyza-pi commented Nov 7, 2024

@ErikEJ The only thing I don't have is the unzipping of the file. I looked and it seems like the code that drops the templates is only called via the GUI. Unless I'm missing something.

@jwyza-pi jwyza-pi marked this pull request as ready for review November 7, 2024 20:31
@ErikEJ
Copy link
Owner

ErikEJ commented Nov 7, 2024

There is a T4Helper class (I also missed that when updating to EF Core 9)

@jwyza-pi jwyza-pi marked this pull request as draft November 7, 2024 20:58
Adds T800 Zip File unzipping for CLI.
@jwyza-pi jwyza-pi marked this pull request as ready for review November 7, 2024 21:17
@jwyza-pi
Copy link
Contributor Author

jwyza-pi commented Nov 7, 2024

Ok, I think it's ready now. I did not add .Net 9 versions of the split entities largely because I don't have any of the net9 stuff setup atm.

samples/CodeTemplates/EFCore/EntityTypeConfiguration.t4 Outdated Show resolved Hide resolved
samples/efcpt-schema.json Outdated Show resolved Hide resolved
src/Core/efcpt.8/HostedServices/ScaffoldHostedService.cs Outdated Show resolved Hide resolved
src/GUI/RevEng.Shared/Cli/Configuration/CodeGeneration.cs Outdated Show resolved Hide resolved
src/GUI/RevEng.Shared/Cli/T4Helper.cs Show resolved Hide resolved
src/GUI/lib/T4_800_Split.zip Outdated Show resolved Hide resolved
@ErikEJ
Copy link
Owner

ErikEJ commented Nov 8, 2024

This is great, just added a few comments

Adds use-t4-split to the VS schema json.
Moves the warnings about use-t4-split + use-t4 to ReverseEngineerRunner
Fxes UseT4Split JsonPropertyOrder being the same as UseT4
Removes sample EntityTypeConfiguration now that it exists in the 800_split folder.
@jwyza-pi jwyza-pi requested a review from ErikEJ November 8, 2024 12:34
Copy link
Owner

@ErikEJ ErikEJ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is a great addition

@ErikEJ
Copy link
Owner

ErikEJ commented Nov 8, 2024

I plan to release a new non-nightly version sometime next week after .NET 9 RTW

@ErikEJ ErikEJ merged commit f52f9da into ErikEJ:master Nov 8, 2024
2 checks passed
@jwyza-pi jwyza-pi deleted the EntityTypeConfiguration-support branch November 8, 2024 12:42
@jwyza-pi jwyza-pi changed the title WIP: Adds support for Using EntityTypeConfiguration files. Adds support for Using EntityTypeConfiguration files. Nov 8, 2024
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

Successfully merging this pull request may close these issues.

2 participants