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

c#-EF核心动力工具反向工程师在不同的SQL服务器上的模型上创建不同的属性

(c# - EF Core Power Tools Reverse engineer create different attributes on model on different SQL servers)

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

我正在使用EF Core Power Tools通过在Visual Studio .Net中选择EF Core Power Tools->反向Enegineer来生成模型。

我仍在使用EF Core 2.x,因此我从第一个对话框中删除了选中标记,你可以在其中选择使用EF Core3.x。

在最后一个选项对话框中,我选择:

  • 使用DataAnnotation属性来配置模型。
  • 使用Handlebars模板(C#)自定义代码。

我正在为数据库使用SQL Server。

我的问题是,当我在本地数据库上运行工具时,生成的模型与另一台服务器上的数据库版本不同。

区别在于生成的POCO类。在我的本地数据库上,生成以下类:

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

当我针对另一个数据库运行该工具时,代码如下所示:

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

为什么该工具生成的模型在一个数据库上的dbo表上具有属性,而在另一数据库上却没有属性?

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

正如@Karan所说,这是因为用于脚手架的用户没有dbo作为默认架构。

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