question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EFCore 5 reverse engineering resulting in broken using statements for some models

See original GitHub issue

Our generated models are now showing unnecessary (and invalid) using statements after running through the Reverse engineering process.

image

This is not occurring for all of the generated models; there is no clear pattern for which ones are affected by this and which ones are not.

The exact reproduction steps are listed below. I would like to call out that not checking the Customize code using Handlebars templates does not produce the unnecessary and broken using statements.

Our Handlebars templates have not changed since 2020, but for completeness I will include them below:

CodeTemplates/CSharpEntityType/Class.hbs

{{> imports}}

namespace {{namespace}}
{
    {{#if comment}}
    /// <summary>
    /// {{comment}}
    /// </summary>
    {{/if}}
    {{#each class-annotations}}
    {{{class-annotation}}}
    {{/each}}
    public partial class {{class}}
    {
        {{{> constructor}}}
        {{{> properties}}}
    }
}

and

CodeTemplates/CSharpEntityType/Partials/Imports.hbs

using System;
using System.Collections.Generic;
{{#if use-data-annotations}}
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
{{/if}}
{{#each imports}}
using {{import}};
{{/each}}

Steps to reproduce

No changes were introduced to our Azure MS SQL database. We go through the Reverse Engineering steps as follows:

  • No filter schemas
  • EF Core 5 selected
  • Variety of tables are chosen for Database Objects.
  • Context name, namespace, entity types path, dbContext path are given.
  • Use table and column names directly from the database is checked.
  • Customize code using Handlebars templates for C# is checked (we believe this to be part of the problem)
  • Split DbContext into Configuration classes is checked.
  • No other options are checked, we generate our models.

Screenshots of the exact settings below, if that is more useful:

image

image

image

image

Further technical details

EF Core Power Tools version: 2.5.758.0

Database engine: Azure SQL

Visual Studio version: Microsoft Visual Stuio Community 2019: Version 16.11.2

dotnet SDK version: 5.0.400

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:37 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
clagraffcommented, Sep 24, 2021

Hi @ErikEJ 👋

I can confirm everything is looking correct w/ the latest daily. No unexpected using statements. Thanks a bunch for your help in resolving this.

1reaction
ErikEJcommented, Sep 22, 2021

I think I need a repro database schema so I can investigate locally.

Creation of CodeTemplates folder should be automatic, I will investigate why you are getting errors.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework Models are not working after Reverse ...
I tried to use the reverse engineering method as mentioned here. I had to do that because of the changes in a few...
Read more >
Entity Framework Core 5 - Pitfalls To Avoid and Ideas to Try
The biggest concerns of reverse engineering an existing database come from concept shifts that occur from a diverging code base and database ...
Read more >
Scaffolding (Reverse Engineering) - EF Core
Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema.
Read more >
What's New in EF Core 6.0
EF Core 6.0 contains several improvements when reverse engineering an EF model from an existing database. Scaffolding many-to-many relationships.
Read more >
EF Core 7: It Just Keeps Getting Better
It's no surprise that Julie's excited about the latest EF release. It's faster, it allows bulk updates and deletes, it lets you map...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found