• ASP.NET Core

    EF Migration tasks in the Program.cs – The good and the Ugly

    Program.cs of any core project (especially ASP.Net Core) is our first starting point to get some necessary work out of the wrong. In this case EF DB Migrations. You know that EF thing where you keep your attached DB tables in sync with your data models? (or in some cases… mess up your existing database).

    Life in program.cs normally starts here:

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

    But let’s consider this:

     

    public class Program
    {
        public static async Task Main(string[] args)
        {
            IWebHost webHost = CreateWebHostBuilder(args).Build();
    
            // Create a new scope
            using (var scope = webHost.Services.CreateScope())
            {
                // Get the DbContext instance
                var myDbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
    
                //Do the migration asynchronously
                await myDbContext.Database.MigrateAsync();
            }
    
            // Run the WebHost, and start accepting requests
            // There's an async overload, so we may as well use it
            await webHost.RunAsync();
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

     

    What the? So what is going on here… Well, basically we are injecting our EF DB Migration process between the creating of our web app (Build) and the actual Running of the site… You know that period of time when processes are defined and setup and actually begin listening on the wire.

     Have a look at this article….

    https://andrewlock.net/running-async-tasks-on-app-startup-in-asp-net-core-part-1/

     

     

      

  • Microsoft

    EF Core adds DB Seeding back with some gotchas

    Looking back down the latest with EF core (in my case 2.1.4), I notice some gotchas when it comes to Data Seeding….

    So basically when you involve your migrations with using .HasData it’s understood that you must provide a initial value for all columns that are keys.
      **Side note: this comes from the EF Core team that they want to maintain consistency across multiple migrations. Well, I can understand their reasoning, but I think it’s unnecessary, because we are talking about in most cases initial testing data. I get it.

    With this requirement in mind, this brings up some problems with seeding. In particular with data classes that have constructors and entities with private setters. Consider the example (taken from a PluralSight course on this very topic):

    public class Location
    {
        public int LocationId { get; private set; }
        public string StreetAddress { get; set; }
        public string OpenTime { get; set; }
        public string CloseTime { get; set; }
        public List<Unit> BrewingUnits { get; set; }
        public Location(string streetAddress, string openTime, string closeTime)
        {
            StreetAddress = streetAddress;
            OpenTime = openTime;
            CloseTime = closeTime;
        }
    }

    For locationId we have a private setter, because this will be handled on the DB as Id type key. Well when it comes to doing a DataSeed:

    modelBuilder.Entity<Location>().HasData(
     new Location
      {
            LocationId = 1,
            StreetAddress = "1 Main Street",
            OpenTime = "6AM",
           CloseTime = "4PM"
        });

    This code is going to have a problem, because for New Location, the private setter will throw a compiler error, yeap. But EF Core HasData must be able to set ALL key values. Yeah, how to paint oneself into a corner.

    Well, anonymous types to the rescue!

    modelBuilder.Entity<Location>().HasData(
     new {
          LocationId = 1,
          StreetAddress = "1 Main Street",
          OpenTime = "6AM",
          CloseTime = "4PM"
         });

    It becomes anonymous because Location type is removed from the new statement- the Location type is already known because of .Entity of Location type- so EF Core already knows how to build the migration.

    If we look at the actual migration code:

    migrationBuilder.InsertData(
     table: "Locations",
      columns: new[] { "LocationId", "CloseTime", "OpenTime", "StreetAddress" },
               values: new object[] { 1, "4PM", "6AM", "1 Main Street" });

    We see the key is getting set- that will make EF Core happy.

    Is this a good or bad way to deal with this? It depends on your take on anonymous types. It does feel a little kluge like. But again, this comes from how the EF Core team decided to handle HasData seeding when it comes to keys. Btw, foreign keys are not excluded from this requirement. 

  • Microsoft,  Old Tech

    So the begin the long end of Windows 8.1

    Has the time come? The Windows “Vista and ME” of modern operating systems finally nearing end of life. Yes, Windows 8.x will soon join the list of… well let us say… “not best operating systems out there” editions.

    The only good I ever saw about the Windows 8.x code base was when the Mobile version finally moved windows 7.x mobile forward. Well, that is until Microsoft decided to stop doing mobile. A very sad day when I shutdown my Nokia 1520- It was an excellent phone. Just before I moved my wonderful 1520 to dust shelf of non-supported tech, I was able to get it onto Windows 10 mobile- I even wrote a couple of apps for it. My my…

    But I must say, while not the best effort, Windows 8.x did move the OS in new directions. Which needed to happen. My work PC is still a Windows 7 machine (you know that OS that is almost 10 years old)… I’m back in the days with my corporate PC, being reminded how long it took to get past Windows XP… some IT shops are just so slow in moving forward.

    The next mission, getting Windows 7 out of my tech life. Windows 10, it’s a keeper… I see as one of the good editions of Windows. And yes, in it’s day…. Windows XP and Windows 7 where great OS’s for their time. But tech has to move forward.

  • Microsoft

    Windows UI Library Preview released! – Windows Developer Blog

    Windows UI Library Preview released! – Windows Developer Blog

    Windows UI Library Preview released! – Windows Developer Blog

    We’re excited to announce the first preview release of the Windows UI Library! The Windows UI Library (or WinUI for short) is a new way to get and use Fluent controls and styles for building Windows 10 UWP apps: via NuGet packages. The WinUI NuGet packages contain new and popular UWP XAML controls and features …

    Source: blogs.windows.com

  • Microsoft,  UWP

    Windows Community Toolkit Major update

    In the “… case you didn’t hear department…”, looks like UWP Toolkit or are we just now calling it Windows Community Toolkit got a major revision…

    Check it out:

    WindowsCommunityToolkit – The Windows Community Toolkit is a collection of helper functions, custom controls, and app services. It simplifies and demonstrates common developer tasks building UWP ap…

     

    And… on Docs:

    The Windows Community Toolkit is a collection of helper functions, custom controls, and app services. It simplifies and demonstrates common developer tasks building UWP apps for Windows 10.