EF Core Power Tools

repository·master·Indexed 25 days ago

https://github.com/erikej/efcorepowertools

A suite of tools to simplify EF Core development, featuring a Visual Studio 2022 extension for GUI-based reverse engineering and DbContext visualization, and a cross-platform CLI (efcpt) for other environments. Includes the ErikEJ.EntityFrameworkCore.DgmlBuilder for generating DGML graphs and ErikEJ.EntityFrameworkCore.SqlServer.Dacpac for reverse engineering SQL Server .dacpac files.

Tokens
12.1K
Snippets
36
Records
66
Agent score
81%

What's inside EF Core Power Tools

  1. Use database names with or without inflection

    master

    When using UseDatabaseNames, you can further control how names are handled using the UseInflector setting in efpt.config.json:

    1. UseDatabaseNames: true and UseInflector: false: The tool uses the exact database names. For example, a table named Alphabetical_list_of_products will result in a class named Alphabetical_list_of_products and a property ProductID (if the column is ProductID).
    2. UseDatabaseNames: true and UseInflector: true: The tool uses database names but applies inflection rules (e.g., handling singular/plural forms). For example, a table named Alphabetical_list_of_products might result in a class named Alphabetical_list_of_product (singularized).
    // Example 1: Exact database names
    {
       "UseDatabaseNames": true,
       "UseInflector": false
    }
    
    // Example 2: Database names with inflection (singularization)
    {
       "UseDatabaseNames": true,
       "UseInflector": true
    }
  2. How StatusbarHelper works with custom layouts

    master

    If you want full control over your status bar's XAML layout, use the StatusbarHelper instead of the StatusbarControl. The helper provides the logic for updating icons and text, but requires you to provide the UI elements.

    Requirements for Custom Layouts

    1. An Icon Image control: An Image control that the helper can update.
    2. A Main TextBlock: A TextBlock control for the primary status message.
    3. Resource Dictionary: If using default icons, you must include icons.xaml in your Window or App resources.
    4. Fixed Dimensions: For best visual results, use a fixed height for the StatusBar and the icon (13-15px recommended) to prevent layout flickering during animations.

    Implementation Steps

    1. Define your custom StatusBar in XAML with named Image and TextBlock controls.
    2. Create a property in your Window/Control to hold the StatusbarHelper instance.
    3. Initialize the helper in your constructor, passing the named text and icon controls.
    <!-- 1. Add icon resources -->
    <Window.Resources>
     <ResourceDictionary Source="pack://application:,,,/Westwind.Wpf.Statusbar;component/Assets/icons.xaml" />
    </Window.Resources>
    
    <!-- 2. Define custom layout -->
    <StatusBar Height="30" VerticalAlignment="Bottom" HorizontalAlignment="Stretch">
        <StatusBar.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto"  />
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </StatusBar.ItemsPanel>
    
        <StatusBarItem Grid.Column="0" Margin="2,1,0,0">
            <Image x:Name="StatusIcon" Source="{StaticResource circle_greenDrawingImage}" Height="14" />
        </StatusBarItem>
        
        <StatusBarItem Grid.Column="1">
            <TextBlock Name="StatusText">Ready</TextBlock>
        </StatusBarItem>
        
        <StatusBarItem Grid.Column="2">
            <ContentControl Name="StatusCenter" x:FieldModifier="public" />
        </StatusBarItem>
        
        <StatusBarItem Grid.Column="3">
            <ContentControl x:Name="StatusRight" x:FieldModifier="public" />
        </StatusBarItem>
    </StatusBar>
  3. Configure and override status bar icons

    master

    The status bar uses ImageSource objects for icons. You can customize them in three ways:

    1. Globally: Override StatusIcons.Default properties. This must be done before any controls or helpers are created to affect all instances.
    2. Per Control/Helper: Assign a new StatusIcons instance to the StatusbarControl.Status.StatusIcons or StatusbarHelper.StatusIcons property. It is recommended to create a new instance to avoid modifying the global defaults.
    3. Per Call: Pass a specific ImageSource to the imageSource parameter of any ShowStatusXXX() method.
  4. Retrieve certificate fingerprint and key container ID

    master

    To sign VSIX extensions, you need to extract specific metadata from your certificate:

    1. Key container ID (-k) and CSP (-csp): Use certutil to find these values for your certificate: certutil -user -store my "<certificate serial number>"

    2. Certificate fingerprint (-cfp):

      • Save your certificate as a binary .cer file.
      • Run the following PowerShell command to get the SHA256 hash:

      Get-FileHash -Algorithm SHA256 <path to .cer file> | Format-Table -AutoSize

    certutil -user -store my "<certificate serial number>"
    
    # PowerShell for fingerprint
    Get-FileHash -Algorithm SHA256 <path to .cer file> | Format-Table -AutoSize
  5. Use EF Core Power Tools CLI (efcpt) for cross-platform reverse engineering

    master
    If you are not using Visual Studio (for example, if you are using Visual Studio Code), you can use the efcpt dotnet tool. This is a cross-platform command-line tool designed for reverse engineering databases.
  6. Customize the DbContext class name

    master

    By default, EF Core Power Tools may propose a DbContext name based on your database name. You can override this to name the derived DbContext after your specific domain model instead. To change the name, update the ContextClassName property in your efpt.config.json file. Note that changing this value will result in the generated C# file being renamed to match the new class name.

    "ContextClassName": "CustomContext"
  7. Install the EF Core provider package in the project

    master

    To use EF Core Power Tools features that require a specific database provider, you must ensure the relevant EF Core provider package is installed in your project. This is suggested when the provider is missing from your project configuration. For example, if you are working with SQL Server, you need to add the Microsoft.EntityFrameworkCore.SqlServer package to your .csproj file.

    <ItemGroup>
      <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.5" />
    </ItemGroup>
  8. Install the EF Core Power Tools CLI

    master

    Install the efcpt command-line tool globally using the dotnet tool install command. Choose the version that matches your target EF Core version:

    • EF Core 10: Use version 10.*
    • EF Core 9: Use version 9.*
    • EF Core 8: Use version 8.*

    The tool requires the .NET 8.0 or .NET 10.0 runtime to be installed on your operating system.

  9. Organize generated code using OutputPath and ModelNamespace

    master

    You can use the OutputPath and ModelNamespace configuration options in efpt.config.json to organize your project folder structure and ensure generated code is separated into a specific directory.

    • OutputPath: Specifies the subfolder where generated entity files will be placed (e.g., "Models").
    • ModelNamespace: Specifies the additional namespace segment to append to the project's base namespace for the generated entities (e.g., "Entities").

    When both are used, the files are moved to the specified folder, and the C# namespace declaration is updated to include the ModelNamespace suffix.

    {
       "ModelNamespace": "Entities",
       "OutputPath": "Models"
    }