Coming up to speed with Entity Framework Core 2.x



Having used Entity Framework since 4.x, I have seen plenty of examples of what works and what doesn't regarding this library.  One of the things that has been a big headache is using the EDMX designer.  This is primarily related to using source control.  This part of EF will lead to scenarios where code can not be merged easily back into a main branch.  Since this was on an existing project, it couldn't be done away with immediately due to time constraints. 

Looks like I'm not the only one, judging from the comments on this article.
https://forums.theregister.co.uk/forum/1/2014/10/23/entity_framework_goes_codefirst_only_as_microsoft_shutters_yet_another_visual_modelling_tool/


[ update 4/10/2019 ]
The solution for dealing with the EDMX designer, was to use an extension called "Reverse POCO Generator"
https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator


This extension lets you have a code-first, EDMX-free approach with an existing database. See the docs at the link for more info.  I've been using it well over a year now.  However, its unnecessary for EF Core now.

There are plenty of examples out there about using the Dotnet CLI for EF Core, but here is just an example for the command you will need to get familiar with.

dotnet ef dbcontext scaffold "Server=MyDbInstance;Database=MyDbName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -c MyDbContextName -d -f -o Entities

Notice that this is using a trusted connection, as opposed to your actual application context might use either sql auth or windows auth with the app pool running as a domain account(preferred).  The "workflow" will involve using this command after updating your database schema.  Contrast this with EF 6.x and the Reverse POCO generator, which will require you to "touch" a TT(T4 Template) file to initiate a rebuild of the db model.







Angular vocabulary

If you're still not on the bandwagon for using Angular yet, the learning curve can be steep.  One of the things that comes with any new framework is the vocabulary involved.  So here is a list of the terms you will need to get familiar with.  One of the go-to resources to start is angular.io

Component
Module
Service
Template 
Route








Why I prefer entity framework to stored procedures



Why Entity Framework (and ORM in general) instead of stored procedures? Below is a an article that pretty well sums up my thoughts on this matter as well.  Believe it or not, the debate over the use of an ORM with respect to data access for an application still comes up.


https://kevinlawry.wordpress.com/2012/08/07/why-i-avoid-stored-procedures-and-you-should-too/

 To summarize my points:
(1) This is the big one - compile time checking.  If your underlying schema changes (e.g. rename a column), and you missed one of the stored procedures that use it, the error will be evident only when hitting that code at run-time.  If instead, you used an ORM like Entity Framework, this will be caught at compile-time.
(2) There is still a place for stored procedures, but the vast majority of your data access code will likely be get-something(s) and sort it.  Its frustrating to maintain dozens of stored procedures that do little other than fetch a list of customers.
(3) Looping.  TSQL is optimized to operate on sets of data.  Yes, you can write a while loop in TSQL, but wouldn't you rather do this with C#?  It's more often the case, that something done in TSQL can just as easily be done with LINQ and EF, and usually cleaner.

There are more, those are probably the big three.

With regards to Entity Framework, there are generally 2 approaches - code first and database first.  Each has its merit, but if you use any kind of source control (and if you're not shame on you), then the database first approach presents a problem with what's known as concurrent development. What this means is that you branch your code and start making changes, and something requires a change in the main branch, merging the EDMX file will often result in conflicts.  These days, I prefer the code first approach, but with an existing database, preferring to design the schema myself so as to control the data types and FK relations myself.  To do this, I now use extensions such as the Reverse POCO generator.  This lets me do concurrent development, while letting me build my own schema. The flow mor eor less goes like this:
- Make SQL changes
- Reverse the EF model changes via the POCO tool
- Code against the updated EF model




Is the .NET Core pie fully baked yet?

Overview of .NET Core vs .NET Framework
I must admit, I have not thought too highly of the whole .NET core effort that has been going on the last 2 years or so.  When Microsoft changes its technology stack, its often a big bucket of Legos dumped on you all at once.  However, this time it was even more confusing because there was more than one bucket! That said, I have had the view that this was the direction things were headed for a while, regardless of Microsoft's claim that they will continue to develop both the framework and .net core stacks. Its hard to imagine them eventually not developing things to the point they are one set of API's, which isn't the case now. That might mean yet another name change...

Where are we now?
As it stands, you can still develop a new application with MVC5 or use MVC6 (ASP.NET core) with the full framework, or use the .NET core libraries.  Confused yet?  Then there is .NET Standard.  Uh oh. I'm not the only one. However, things seem to be settling down now, and at least for new applications, ASP.NET Core might just make sense.  If for no other reason, than the new project structure which makes using something like Angular and associated NPM libraries much easier.
 
Where is this headed?
Good question.  Even though Microsoft claims they will continue to develop the full framework AND .net core, its likely that the focus will be on the .net core stack.  Just a guess.

When to choose one over the other. link



Angular and ASP.NET MVC hybrid application

When I first started looking at Angular it was still called AngularJS.  Then came Angular2 and Typescript.  At the same time, Microsoft was busy turning over all the boxes of .NET leading to utter chaos. As is the case in application development every few years, new and disruptive technology comes along that requires investments of time to learn.  When I started using Angular, it has been in the context of being delivered via an ASP.NET application.  This complicates things even further, since with MVC, there is routing involved and its own application life-cycle.  Then there are the Angular module loaders.  With .NET core, things get a little more streamlined with less work-arounds.  If you're still using MVC5 or earlier, I found it easier to use System.js as opposed to the other popular loader known as webpack.  Here is a good example of using Angular within an ASP.NET MVC (not .NET core) application

http://hive.rinoy.in/angular4-and-asp-net-mvc-hybrid-application/


Add appsettings.json to .NET 6 Console App

  When you start a new .NET 6 Console app, you will have little more than what you see here.   If one wants to use the json configuration fi...