Database wizard UseDatabaseNames etc

Hello,

If I refresh the model from the database with the database wizard all my classes have other naming conventions:

Old Model:

[Table("Pfuscheraktinhalt", Schema = "dbo")]
public partial class Pfuscheraktinhalt
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Required]
public int Akt_ID { get; set; }

    public Pfuscherakt Pfuscherakt { get; set; }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Dokument_ID { get; set; }

    [Required]
    public int Dokumenttyp_ID { get; set; }

    public Dokumenttyp Dokumenttyp { get; set; }

    [Required]
    public string Dokumentname { get; set; }

    [Required]
    public byte[] Dokumentinhalt { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public string Dokumentgröße { get; set; }

    public bool Intern { get; set; }

    [Column(TypeName="datetime2")]
    [Required]
    public DateTime angelegt { get; set; }

    [Column(TypeName="datetime2")]
    public DateTime? geändert { get; set; }

    public string Benutzer { get; set; }
}

now model:

[Table("Pfuscheraktinhalt", Schema = "dbo")]
public partial class Pfuscheraktinhalt
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Column("Akt_ID")]
[Required]
public int AktId { get; set; }

 public Pfuscherakt Pfuscherakt { get; set; }

 \[Key\]
 \[DatabaseGenerated(DatabaseGeneratedOption.Identity)\]
 \[Column("Dokument_ID")\]
 public int DokumentId { get; set; }

 \[Column("Dokumenttyp_ID")\]
 \[Required\]
 public int DokumenttypId { get; set; }

 public Dokumenttyp Dokumenttyp { get; set; }

 \[Required\]
 \[MaxLength(255)\]
 public string Dokumentname { get; set; }

 \[Required\]
 public byte\[\] Dokumentinhalt { get; set; }

 \[DatabaseGenerated(DatabaseGeneratedOption.Computed)\]
 \[MaxLength(35)\]
 public string Dokumentgröße { get; set; }

 public bool Intern { get; set; }

 \[Column("angelegt",TypeName="datetime2")\]
 \[Required\]
 public DateTime Angelegt { get; set; }

 \[Column("geändert",TypeName="datetime2")\]
 public DateTime? Geändert { get; set; }

 \[MaxLength(100)\]
 public string Benutzer { get; set; }

}

here my settings from appsettings.json

"Databases": {

"Pdb": {

  "NoPluralize": true,

  "UseDatabaseNames": true,

  "UseEFNaming": false,

  "PrefixNavigation": false

}

how to have the same naming conventions as in the old model after refresh?
because now I get hundrets of errors…
Robert

Hi @Mad.Rain,

Radzen Blazor Studio reads the database settings from both appsettings.json and appsettings.Development.json, and the Development file takes precedence. If the "Databases" section there is missing or has different values, the default Entity Framework naming is used, which is what you are seeing.

Please make sure appsettings.Development.json contains the same settings:

"Databases": {
  "Pdb": {
    "NoPluralize": true,
    "UseDatabaseNames": true,
    "UseEFNaming": false,
    "PrefixNavigation": false
  }
}

Then refresh the model again. If the problem persists, please send us your appsettings.Development.json and the line in Program.cs that calls GetConnectionString.

I have done that - it seems that the wizard overwrites it and after using the wizard I cannot start it again (because of the errors):

here my appsettings.Development.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "ConnectionStrings": {
    "PDBConnection": "Server=xxx;Connection Timeout=30;Persist Security Info=False;TrustServerCertificate=True;Integrated Security=True;Initial Catalog=PDB",
    "PDBAuthConnection": "Server=xxx;Connection Timeout=30;Persist Security Info=False;TrustServerCertificate=True;Integrated Security=True;Initial Catalog=PDBAuth",
    "PdbConnection": "Server=xxx;Connection Timeout=30;Persist Security Info=False;TrustServerCertificate=True;Integrated Security=True;Initial Catalog=PDB"
  },
  "IdentityServer": {
    "Clients": {
      "PdbDetektive.Client": {
        "Profile": "IdentityServerSPA"
      }
    }
  },
  "Databases": {
    "Pdb": {
      "NoPluralize": true,
      "UseDatabaseNames": true,
      "UseEFNaming": false,
      "PrefixNavigation": false
    }
  }
}

Thanks for the details! We were able to reproduce this. The wizard reads the naming options from the "Databases" section by matching the connection string name used in the AddDbContext registration in Program.cs. When it can't find a match it falls back to the defaults (EF Core naming), which is why the properties came out as AktId instead of Akt_ID, and then it writes those defaults back to appsettings.Development.json.

There are two cases where the match fails:

  1. The connection string is stored in a variable before being passed to UseSqlServer() / UseNpgsql() etc. instead of options.UseSqlServer(Configuration.GetConnectionString("PdbConnection")) inline.

  2. The key under "Databases" doesn't exactly match the connection string name minus the "Connection" suffix, including letter case (e.g. PDBConnection vs. Pdb).

Could you check which one applies to your Program.cs and appsettings? As a workaround, using the inline GetConnectionString("PdbConnection") form with a Databases.Pdb key of the same casing will make the wizard pick up your settings. We will fix the lookup so that both cases work in the next release.

this look OK:

services.AddDbContext<PdbDetektive.Data.PdbContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("PDBConnection"));
            });

maybe it is because of this (PDB and Pdb):

 "ConnectionStrings": {
    "PDBConnection": "Server=xxx;Connection Timeout=30;Persist Security Info=False;TrustServerCertificate=True;Integrated Security=True;Initial Catalog=PDB",
 
"Databases": {
    "Pdb": {
      "NoPluralize": true,
      "UseDatabaseNames": true,
      "UseEFNaming": false,
      "PrefixNavigation": false
    }

Robert