Set Node To Specific Database

<< >>

Navigation:  CodeStencil > Code Tree > Context Menu >

Set Node To Specific Database

 

The "Set Node To Specific Database" is used to assign nodes to specific databases. Whether these nodes are generated or not is dependent on the database the project is set to use.

 

 

CS_clip0074

 

 

In most cases, you want your stencil to support different types of databases, e.g, Sql Server, MySql, Sqlite. In order to be able to achieve this, an option (even though, more complicated) would be to create a Code Nanite to handle this. On the other hand a more simple or subtle approach will be to create the files for the different databases and CodeStencil can triggered to use the matching file for the current database type in use.

 

Take a look at this code snippet from a Startup.cs file.

 

using System.Threading.Tasks;

using Microsoft.Extensions.Configuration;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Hosting;

using [%NAMESPACE%].Models;

 

namespace [%NAMESPACE%]

{

   public class Startup

   {

       public Startup(IConfiguration configuration)

       {

           Configuration = configuration;

       }

 

       public IConfiguration Configuration { get; }

 

       // This method gets called by the runtime. Use this method to add services to the container.

       public void ConfigureServices(IServiceCollection services)

       {

           services.AddDbContext<[%DB_CONTEXT%]>(options =>

               options.UseSqlite(

                   Configuration.GetConnectionString("[%DB_CONTEXT%]")));

           services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)

               .AddEntityFrameworkStores<[%DB_CONTEXT%]>();

           services.AddRazorPages();

       }

 

       // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

       public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

       {

           if (env.IsDevelopment())

           {

               app.UseDeveloperExceptionPage();

               app.UseDatabaseErrorPage();

           }

           else

           {

               app.UseExceptionHandler("/Error");

               // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.

               app.UseHsts();

           }

 

           app.UseHttpsRedirection();

           app.UseStaticFiles();

 

           app.UseRouting();

 

           app.UseAuthentication();

           app.UseAuthorization();

 

           app.UseEndpoints(endpoints =>

           {

               endpoints.MapRazorPages();

           });

 

       }

   }

}

 

 

In the case of a Sql Server project, the highlighted text would be "UseSqlServer" instead. So all we need to do is to create another "Startup.cs"node with "UseSqlServer" and then use the context menu to assign which database matches the node.

 

CS_clip0075

 

Now, how does CodeStencil figure out which node to use at generation time?

 

You will notice from the node that in addition to the node label, there is a concatenated string - {Sqlite}, {SqlServer}. CodeStencil is able to figure out which one to use because there is an Expander called DATABASE that is updated during Schema Import.

 

CS_clip0077

 

During code generation, when CodeStencil hits a node with a label containing  a Bracer, it will extract the text and then check to see if it contains a database type, e.g. SqlServer, Sqlite, MySql, etc. It then matches it to the expander - DATABASE. If it matches, only then will that node be generated.