1. Overview
  2. Code Generation
  3. Handling Table Name Violations (Schema Import)

Handling Table Name Violations (Schema Import)

Database Schema Import

It is likely that the database you are trying to import into CodeStencil contains a table with a name that violates our list of reserved names. A common example is the name "Client." While this may seem harmless, certain stencils may adopt similar naming for their projects. See screenshot below:

In a case like this, the generated code will be conflicted in treating the word "Client" either as a namespace or an object hence the generated code will be riddled with compile errors. Nevertheless, CodeStencil has a way that allows you to transparently rename the errant table (after the fact), and not break the flow of subsequent code generation after a schema has been imported.

Handling Name Violations

The remedy for handling reserved names violations is a post-scaffold solution. This means that the scaffolded files are modified before they are used to update the Schema Dictionary.

During Schema Import, when a table with a name that violates reserved keywords is encountered, you will get a prompt explaining that your database contains tables violating reserved names:

If you click No, it will bypass the fix and proceed to update the Schema Dictionary. If the specific stencil you are using does not generate "Client" as part of the project namespace then your project should compile without any errors.

If you click Yes, you will be presented with grid that will allow you supply an alternative name for the errant table:

Click under the "New Name" column to supply the new name. Click OK when done. You will be asked to confirm that you want to apply the changes one last time:

Click Yes to accept.

 

What Is Updated

In the DbContext class, the following changes are made:

  1. public virtual DbSet<MyClient> MyClients {get; set;} - "Client" is replaced with "MyClient"
  2. public virtual DbSet<MyClient> MyClients {get; set;} - "Clients" is replaced with "MyClients"
  3. modelBuilder.Entity<MyClient>(entity => - "Client" is replaced with "MyClient"
  4. entity.ToTable("Client",tb => tb.HasComment( - The ToTable property gets the original name of the table, in this case "Client" added so that Entity Framework has a way of updating the table even though the front facing objects have updated names.
  5. entity.HasOne(d => d.MyClient).WithMany(p => p.pools) - "d.Client" is replaced with "d.MyClient".  This update is as a result of the the errant table being used as a foreign key in another table:

 


Was this article helpful?