Warm tip: This article is reproduced from serverfault.com, please click

EF Core Power Tools Reverse engineer create different attributes on model on different SQL servers

发布于 2020-07-09 12:46:53

I'm using EF Core Power Tools to generate my model by choosing EF Core Power Tools -> Reverse Enegineer within Visual Studio .Net.

I'm still using EF Core 2.x, so I remove the checkmark from the first dialog where you can choose to use EF Core 3.x.

In the last options dialog I select:

  • Use DataAnnotation attributes to configure the model.
  • Customize code using Handlebars templates (C#).

I'm using SQL Server for my databases.

My problem is that the model generated is different when I run the tools on my local database compared to a version of the database on another server.

The difference is in the generated POCO class. On my local database, the following class is generated:

public partial class ExampleTable
{
    [Key]
    public byte Id { get; set; }
    public string Tekst { get; set; }
} 

When I run the tool against another database the code looks like this:

[Table("ExampleTable", Schema = "dbo")]
public partial class ExampleTable
{
    [Key]
    public byte Id { get; set; }
    public string Tekst { get; set; }
}

Why is the tool generating the model with attributes on dbo tables on one databse and without attributes on another database?

Questioner
Brian B.
Viewed
11
ErikEJ 2020-07-10 13:25:35

As @Karan said, this is because the user used for scaffolding does not have dbo as default schema.

see https://github.com/dotnet/efcore/blob/master/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs#L174