.Net Core

Matters related to .Net Core

  • .Net Core,  Microsoft

    Dotnet Core Templates, or DOTNET new -i “the magic”

    Time to touch on this topic again… because every time a new DOTNET SDK comes out I seem to lose the new templates. I understand the why, and ya I wish the .Net Core group would fix it. 

    The “why”- each time one updates to a new Core SDK, the template scaffolds get placed into a new folder (usually under your users directory in a annoying place like ….\.templateengine\dotnetcli\”version of sdk”\). So each time you move to a new DOTNET SDK, this becomes a problem.

    The second part of of DOTNET new -i that is an issue, is if you have different nuget repositories set in VS. It’s a great feature in VS 2017 where one can add additional package management sources… problem is with the DOTNET command line when you want to bring in new templates.

    So, why am I going on about this? Well as I move from SDK version to version… I would like the damn templates I’ve added to either get updated (the standard ones do), or at least make sure they are available to the new SDK….  How hard can it be….

    So here’s the list of what I keep installing from (dotnet Templates on github)

    Here’s my list:

    dotnet new -i “Microsoft.AspNetCore.SpaTemplates::*”    – this one always

    dotnet new -i “Boxed.Templates::*”  – I’m still undesided about boxed.

    dotnet new -i Microsoft.AspNetCore.Blazor.Templates::*  – nice little framework that has become a 1st class citizen in VS. Make sure you also check out the VS extension.

    dotnet new -i “RaspberryPi.Template::*” – who does not like Iot stuff. Also available in VS as a 1st class citizen.

    Update: So as we now have SDK version… 2.1.400 I notice that some of the SPA templates remain off the list…

    The SPA templates after SDK update:
    ASP.NET Core with Angular angular [C#] Web/MVC/SPA
    ASP.NET Core with React.js react [C#] Web/MVC/SPA
    ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA

    And the templates after the dotnet new -i of the SPA’s:

    ASP.NET Core with Aurelia aurelia [C#] Web/MVC/SPA
    ASP.NET Core with Knockout.js knockout [C#] Web/MVC/SPA
    ASP.NET Core with Vue.js vue [C#] Web/MVC/SPA
    ASP.NET Core with Angular angular [C#] Web/MVC/SPA
    ASP.NET Core with React.js react [C#] Web/MVC/SPA
    ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA

    The ones in bold seem to gone still…. oh well.

  • .Net Core,  Microsoft

    .net Core 2.0 and SQL via EF Core 2.0

    So I can never remember the order that these goes in so Here we go:

     A console App with .net Core/SQL Server/EF Core 2.0

    – In VS 2017 – Create a in .Net Core a Console App (.NET CORE)
    -Package Manager Console: 

    Install-Package Microsoft.EntityFrameworkCore.SqlServer  [Current Version was 2.0.1]
    Install-Package Microsoft.EntityFrameworkCore.Tools  [Current Version was 2.0.1]
    Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design  [Not sure this one is required]

    Now let’s say we have a database and table…. Let’s reverse engineer it!

    Scaffold-DbContext “<that magic connect string>” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
      This is command that will use the <that magic connect string> and the provider Microsoft.EntityFrameworkCore.SqlServer with the code generation going into the Models sub-directory. 

    Examples <that magic connect string>

    For local DB and table: “Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;” 

    For a real SQL Server connection instance: 

     Scaffold-DbContext "Server=Orion;DataBase=Blogging;Integrated Security=False;
    User ID=sa;Password=****;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;
    ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
    Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

    Also add the table parameter to get only certain tables   -table Blogs, Post
     

    So that was the Hard part… now some sample code:

    Make sure you add using Microsoft.EntityFrameworkCore; And if you put the dbContext into it’s own folder, make sure you include the class namespace.

    
    using System;
    using Microsoft.EntityFrameworkCore;
    using conAppSqlCore.Models;
    
    namespace conAppSqlCore
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
                using (var db = new BloggingContext())
                {
                    foreach (var blog in db.Blog)
                    {
                        Console.WriteLine($"Name URL - {blog.Url}");
                    }
                }
                Console.ReadLine();
            }
        }
    }