dotnet / EntityFramework.Docs

Documentation for Entity Framework Core and Entity Framework 6
https://docs.microsoft.com/ef/
Creative Commons Attribution 4.0 International
1.59k stars 1.95k forks source link

Scaffolding other types of files: What filename should I put in? #4456

Open doggy8088 opened 10 months ago

doggy8088 commented 10 months ago

In the Scaffolding other types of files, you provided an T4 example, but not mentioned about the filename and where should I put it into. I don't know how to use this Mermaid T4 script.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

bricelam commented 10 months ago

The mermaid script is intended to be used as a custom DbContext.t4. We should mention this in the article.

doggy8088 commented 9 months ago

How to put two DbContext.t4 in the same project? I want to generate both the context and the mermaid file.

bricelam commented 9 months ago

You can't, but you can create a separate dummy project for the diagrams. Or just rename the files before generating.

ErikEJ commented 9 months ago

You can with EF Core Power Tools

doggy8088 commented 9 months ago

@ErikEJ Does EFCPT can handle multiple T4 templates other than DbContext.t4 and EntityType.t4?

doggy8088 commented 9 months ago

The mermaid sample from the doc seems not functional.

image

ErikEJ commented 9 months ago

@doggy8088 Yes, both the VS and CLI editions support that, as documented here

ErikEJ commented 9 months ago

The mermaid sample from the doc seems not functional.

A limitation in the MarkDown viewer?

doggy8088 commented 9 months ago

The mermaid sample from the doc seems not functional.

A limitation in the MarkDown viewer?

No, it's a bug. I fixed as below:

<#@ output extension=".md" #>
<#@ assembly name="Microsoft.EntityFrameworkCore" #>
<#@ assembly name="Microsoft.EntityFrameworkCore.Relational" #>
<#@ assembly name="Microsoft.EntityFrameworkCore.Design" #>
<#@ parameter name="Model" type="Microsoft.EntityFrameworkCore.Metadata.IModel" #>
<#@ parameter name="Options" type="Microsoft.EntityFrameworkCore.Scaffolding.ModelCodeGenerationOptions" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="Microsoft.EntityFrameworkCore" #>
# <#= Options.ContextName #>

```mermaid
erDiagram
<#
    foreach (var entityType in Model.GetEntityTypes().Where(e => !e.IsSimpleManyToManyJoinEntityType()))
    {
#>
    <#= entityType.Name #> {
    }
<#
        foreach (var foreignKey in entityType.GetForeignKeys())
        {
#>
    <#= entityType.Name #> <#= foreignKey.IsUnique ? "|" : "}" #>o--<#= foreignKey.IsRequired ? "|" : "o" #>| <#= foreignKey.PrincipalEntityType.Name #> : "<#= foreignKey.GetConstraintName() #>"
<#
        }

        foreach (var skipNavigation in entityType.GetSkipNavigations().Where(n => n.IsLeftNavigation()))
        {
#>
    <#= entityType.Name #> }o--o{ <#= skipNavigation.TargetEntityType.Name #> : <#= skipNavigation.JoinEntityType.Name #>
<#
        }
    }
#>


![image](https://github.com/dotnet/EntityFramework.Docs/assets/88981/5cc46098-b708-4e34-8089-3a13fa0a2a57)
ErikEJ commented 9 months ago

Maybe a docs PR?

doggy8088 commented 9 months ago

Maybe a docs PR?

Already done https://github.com/dotnet/EntityFramework.Docs/pull/4463

doggy8088 commented 9 months ago

@doggy8088 Yes, both the VS and CLI editions support that, as documented here

@ErikEJ I put a Docs/CodeTemplates/EFCore/DbContext.t4 file. How can I let ContosoUniversityContext.md into Docs/ folder?

image

ErikEJ commented 9 months ago

How can I let ContosoUniversityContext.md into Docs/ folder?

@doggy8088 Currently the files generated by DbContext.t4 are placed in same location as DbContext.cs, but I think it could be changed to the Docs folder. Please create an issue in EF Core Power Tools repo and I will investigate.

ErikEJ commented 9 months ago

@doggy8088 I implemented a fix in the latest source, maybe you can test it from the latest master?