1592829420
More than four years ago, I published a post called Dapper vs Entity Framework vs ADO.NET Performance Benchmarking.
In that post, I determined that Dapper performed markedly better for queries against a SQL Server database than Entity Framework did, and even outperformed ADO.NET in certain cases.
A lot has changed in the .NET world since then, and I figured it might be time for an updated version of this test. Only this time, we’re going to compare Dapper with Entity Framework Core, and run the test in an ASP.NET Core application for good measure.
Let’s see if we can determine whether Dapper or Entity Framework Core performs better for simple queries!
#dapper
1602560783
In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.
To demonstrate CRUD operations – insert, update, delete and retrieve, the project will be dealing with details of a normal bank transaction. GitHub repository for this demo project : https://bit.ly/33KTJAu.
Sub-topics discussed :
In Visual Studio 2019, Go to File > New > Project (Ctrl + Shift + N).
From new project window, Select Asp.Net Core Web Application_._
Once you provide the project name and location. Select Web Application(Model-View-Controller) and uncheck HTTPS Configuration. Above steps will create a brand new ASP.NET Core MVC project.
Let’s create a database for this application using Entity Framework Core. For that we’ve to install corresponding NuGet Packages. Right click on project from solution explorer, select Manage NuGet Packages_,_ From browse tab, install following 3 packages.
Now let’s define DB model class file – /Models/TransactionModel.cs.
public class TransactionModel
{
[Key]
public int TransactionId { get; set; }
[Column(TypeName ="nvarchar(12)")]
[DisplayName("Account Number")]
[Required(ErrorMessage ="This Field is required.")]
[MaxLength(12,ErrorMessage ="Maximum 12 characters only")]
public string AccountNumber { get; set; }
[Column(TypeName ="nvarchar(100)")]
[DisplayName("Beneficiary Name")]
[Required(ErrorMessage = "This Field is required.")]
public string BeneficiaryName { get; set; }
[Column(TypeName ="nvarchar(100)")]
[DisplayName("Bank Name")]
[Required(ErrorMessage = "This Field is required.")]
public string BankName { get; set; }
[Column(TypeName ="nvarchar(11)")]
[DisplayName("SWIFT Code")]
[Required(ErrorMessage = "This Field is required.")]
[MaxLength(11)]
public string SWIFTCode { get; set; }
[DisplayName("Amount")]
[Required(ErrorMessage = "This Field is required.")]
public int Amount { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime Date { get; set; }
}
C#Copy
Here we’ve defined model properties for the transaction with proper validation. Now let’s define DbContextclass for EF Core.
#asp.net core article #asp.net core #add loading spinner in asp.net core #asp.net core crud without reloading #asp.net core jquery ajax form #asp.net core modal dialog #asp.net core mvc crud using jquery ajax #asp.net core mvc with jquery and ajax #asp.net core popup window #bootstrap modal popup in asp.net core mvc. bootstrap modal popup in asp.net core #delete and viewall in asp.net core #jquery ajax - insert #jquery ajax form post #modal popup dialog in asp.net core #no direct access action method #update #validation in modal popup
1603245600
When working with a fast-evolving project changes happen rapidly not only to the project’s code but also to the database schema. It happens especially when working on a micro-service from the start when its purpose shifts.
In Entity Framework Core 5 migrations we are adding a migration as a delta changes between our DbContext
class and existing [DbContext name]ModelSnapshot
. When generating new migration a CLI Tool will generate only the differences between those two and put them in two methods: Up
and Down
. In the first one, there will be a change to apply the migration and in the second one, to remove the migration.
After the migration is applied, its name is noted in the __EFMigrationsHistory
table.
Let’s say that after changing a schema multiple times in the early stages our project is now stable. We have a bunch of migrations that could be merged into one, that would create a model once, without many small updates.
The easiest way to merge all migrations would be removing everything! What I mean is:
__EFMigrationHistory
tableThis is a drastic way of merging migrations because we will lose all the data. However, it’s super simple and it might work in some cases.
This means that we cannot remove all already created database objects, but we can merge migration files in our code. Let’s see how that can be done.
dotnet ef migrations add MergedMigration
Up
and Down
methodsMergedMigration
migration with command dotnet ef database update
MergedMigration
file with earlier generated code#asp.net core for .net 5 & ef core 5 #ef core 5 #ef core migrations #primehotel
1597231440
In Entity Framework Core 5 migrations we are adding a migration as a delta changes between our DbContext
class and existing [DbContext name]ModelSnapshot
. When generating new migration a CLI Tool will generate only the differences between those two and put them in two methods: Up
and Down
. In the first one, there will be a change to apply the migration and in the second one, to remove the migration.
After the migration is applied, its name is noted in the __EFMigrationsHistory
table.
Let’s say that after changing a schema multiple times in the early stages our project is now stable. We have a bunch of migrations that could be merged into one, that would create a model once, without many small updates.
The easiest way to merge all migrations would be removing everything! What I mean is:
__EFMigrationHistory
tableThis is a drastic way of merging migrations because we will lose all the data. However, it’s super simple and it might work in some cases.
#asp.net core for .net 5 & ef core 5 #ef core 5 #ef core migrations #primehotel
1596548640
Database migrations help a developer to keep database schema up-to-date with the code. It is a core mechanism, that keeps changes in code and applies them in the database. Entity Framework Core 5 migrations are designed to keep track of DbContext class and generate migrations when you update it.
To add EF Core migrations, you need to have Entity Framework Core already set up in your project. You can check how to go through that process in this post: PrimeHotel – adding Entity Framework Core 5 in .NET
The easiest way to add and manage migrations is to use .NET Core CLI tools, which you should have already installed. Type this command to check it out:
dotnet tool install –global dotnet-ef
You can also update the tool, once installed:
Adding first migration doesn’t differ much from adding the next ones. You need to open a terminal window in the location of the project and execute the command:
1
dotnet ef migrations add InitialCreate
When this command executes successfully, it will generate a Migrations
directory. InitialCreate
file represents individual migration to match the DbContext. PrimeDbContextModelSnapshot
represents the current state of the model. It is added to the project when the first migration is created and updated with each subsequent migration. It enables the migrations framework to calculate the changes required to bring the database up to date with the model.
In the InitialCreate
file you will find two methods: Up
and Down
. Those will represent the changes when migration will be applied and when it would be rolled back.
#asp.net core for .net 5 & ef core 5 #ef core 5 #ef core migrations #primehotel
1592401691
How to turn DbContext in EF Core to real unit of work that hosts repository instances by injecting querying classes to DbContext.
#.net #ef #core #ef core dbcontext #programming