Sunday, September 30, 2018

how not to do database initialization strategies with Entity Framework

Page 175 of "ASP.NET Core 2 and Angular 5" by Valerio De Sanctis discusses the database management patterns/database initializers/DbInitializers of the early Code-First flavors of Entity Framework before version 7, what is being referred to as EF Core. Out of the box there were four of these:

  • CreateDatabaseIfNotExists
  • DropCreateDatabaseIfModelChanges
  • DropCreateDatabaseAlways
  • MigrateDatabaseToLatestVersion

 
 

Anymore all database initialization stuff is done with PowerShell commands and there are few C# commands. There are three right on the DbContext like so...

  • Database.EnsureCreated()
  • Database.EnsureDeleted()
  • Database.Migrate()

 
 

...but the book eludes to these not being the main focus. I don't know if it will even circle back to them. I'm expecting to be working in PowerShell. Anyways, the four database management patterns/database initializers/DbInitializers could all be inherited from and then extended by way of overriding methods. The book suggests that they were bad because they required a hardcore geek level of knowledge of Entity Framework that was not inclusive to anyone trying to just get their feet wet. From my own experience I found the showstopper for Code First stuff to really lie in this pain point too. I guess the next few pages ahead will allow me to form an opinion as to whether or not anything has gotten better.

Saturday, September 29, 2018

Eden Prairie's Teque Arepa is my new favorite restaurant.

A one-of-a-kind Venezuelan restaurant run by a Venezuelan family, the venue is a welcome addition to a city full of strip malls and chain stores. So far this is the most offbeat thing I have found in generic Eden Prairie. (A coworker recommended it.)

Teque Arepa offers Arepa, a Venezuelan dish of many shapes I had not heard of before. Basically you cut a muffin into the two "bread slices" of a little sandwich and you put something hot and wonderful inside. The dishes here are number 113 Catira Hot (left) and number 106 Pabellón (right) with the beef option.

The wealth of Arpea variants is rivaled by a wealth of smoothie variants. I guess we have some America creeping in with the Venezuela in that regard. My smoothie is a strawberry smoothie.

Why not try it for yourself? It is at 7733 Flying Cloud Drive in Eden Prairie, Minnesota. The zip code is 55344. It's in a strip mall so there must be a suite number, but I can't find it in Googling for it. You'll see it.

Set up a DbContext for the Entity Framework's Code First stuff.

Keeping with the Foo and the Bar object here I think we could have:

using Microsoft.EntityFrameworkCore;
using OurStuff.Data.Models;
namespace OurStuff.Data
{
   public class OurDbContext : DbContext
   {
      public DbSet<Foo> Foos { get; set; }
      public DbSet<Bar> Bars { get; set; }
      
      public OurDbContext(DbContextOptions options) : base(options)
      {
         
      }
      
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
         base.OnModelCreating(modelBuilder);
         
         modelBuilder.Entity<Foo>().ToTable("Foos");
         modelBuilder.Entity<Foo>().Property(f => f.FooId).ValueGeneratedOnAdd();
         modelBuilder.Entity<Foo>().HasMany(b => b.Bars).WithOne(f => f.Foo);
         
         modelBuilder.Entity<Bar>().ToTable("Bars");
         modelBuilder.Entity<Bar>().Property(b => b.BarId).ValueGeneratedOnAdd();
         modelBuilder.Entity<Bar>().HasOne(f => f.Foo).WithMany(b => b.Bars);
      }
   }
}

 
 

This comes from pages 173 and 174 of "ASP.NET Core 2 and Angular 5" by Valerio De Sanctis. Do note the pluralized names for would be database tables. This has forever been the EF way while NHibernate geeks would just have a Foo table for the Foo object.

I saw Luke Schlangen speak at JavaScript MN on Wednesday night on deployments.

Some interesting things were name dropped. GatsbyJS is a React-based tool that allows one to build static web sites quickly. Hugo and Jekyll are other, rival static site generators. Netlify at https://app.netlify.com/ is an intriguing environment where you may quickly host such a site. You may just drag and drop files into the web interface to get them online. You can name a project and Netlify will make a ceremonial little convenience URL for your web site. What is more, if you point the A record for a domain name to Netlify you can, yes, have your domain name resolve to the off-the-cuff hosting and have a real web site by anyone's definition. Maybe this is the new GeoCities, eh? If you don't create an account at Netlify your creations will only last a day, but if you create an account your hosting will be ongoing. Luke opined about how he had packed a bunch of stuff onto Netlify's platform and that they were nonetheless yet to reach out to him for a credit card number or to suggest any sort of payment plan. Another chunk of this talk went into Firebase. "What is the cloud? It's someone else's computer." (Luke verbatim.) We goofed off with Heroku some too. You can't push a Node server up to Firebase or Netlify but in both cases the workaround is to use Google Cloud Functions. These are going to be just what they sound like, some sort of function "out there" that you call out to and get something back from. I don't know how canned versus customizable they are. GitHub Pages is another service for hosting simple static stuff. Luke said that he shied away from using it in his live coding demo as it can take five minutes for your deployments to appear online. Google's Lighthouse will audit your web site and give you performance metrics on how heavy or snappy it is. Matt boasted of how he could get a metric of 90 for one of the simple sites he spun up while google.com itself had a mere 89. Scratch is a very visual scripting language for kids and there is a Scratch IDE. The Scratch IDE, which is web-based, was run through Lighthouse to show off of an example of a performance monster that is going to be judged very harshly by Lighthouse. What else is there to say? Before Luke Schlangen (rightmost in the photo, being introduced by Brandon Johnson) spoke a Terrance Schubring gave a lightning talk on a React component that he had baked. The venue for JavaScript MN has moved to a locale called space150 and this was the first go in the space150 office. The turnout seemed pretty robust in spite of the shakeup.

Friday, September 28, 2018

OpenTextFile in Classic ASP and how to make it "safe" for HP Fortify

This kinda suggests that second, third, and fourth variables may be handed in to restrict the scope of what this can do. The first just specifies the name of the file. The third variable is a true or false variable and handing in false will prevent new files from being created it would seem.

 
 

Addendum 10/5/2018: The better way to go may be to wrap the first variable in mappath(yournamehere) actually.

encrypted stored procedures

This is a thing! You may host databases crafted by third parties that have stored procedures that you cannot read even though it is you that are hosting them.

What is the WebClient of C# 4.0?

It is a kinda ghetto way of hitting API endpoints before the async/await stuff of C# 5.0. The code below is synchronous and just hangs out until we round trip to someone else's server or blow up in exception.

using System.Net;
using System.Text;
using RefreshCerts.Core.ExternalDependencies;
using RefreshCerts.Core.Objects;
namespace RefreshCerts.Infrastructure.ExternalDependencies
{
   public class ApiInteraction : IApiInteraction
   {
      public string GetToken(Bootstrapping bootstrapping)
      {
         using (WebClient webClient = new WebClient())
         {
            webClient.Headers.Add("Content-Type", "application/json");
            var response = webClient.UploadData(bootstrapping.ApiUrl + "authorize",
                  "POST", Encoding.Default.GetBytes("{\"Username\": \"" +
                  bootstrapping.ApiUsername + "\", \"Password\": \"" +
                  bootstrapping.ApiPassword + "\"}"));
            string token = Encoding.UTF8.GetString(response);
            return token;
         }
      }
   }
}

 
 

Honestly, I may abandon this approach and this may be the only blog posting on this subject. I think I went down the wrong rabbithole. Anyhow, some pain points I hit included this error:

The Content-Type header cannot be changed from its default value for this request.

 
 

The fix for this per this is to include the line above that has "application/json" in it. Another error I hit in orginally trying to craft code like this was:

The remote server returned an error: (415) Unsupported Media Type.

 
 

This came from using an .UploadValues implementation instead of an .UploadData implementaion. In .UploadValues the first two parameters handed in are the same (a URL and a verb) and the third is an object like our stuff object here:

var stuff = new NameValueCollection();
stuff["Username"] = bootstrapping.ApiUsername;
stuff["Password"] = bootstrapping.ApiPassword;

 
 

Alright, on the otherside of the two errors above I get:

The remote server returned an error: (403) Forbidden.

 
 

...but that just means my username and password are wrong.