Introduction
ASP.NET Core is a significant redesign of ASP.NET. It is an open source, cross-platform framework to develop Cloud based applications like web applications, IoT applications, and mobile back-end applications. We can develop and run the ASP.NET Core application on multiple (cross) platforms, such as Windows, Mac OS, and Linux. Entity Framework Core is a lightweight and extensible version of Entity Framework. It is based on ORM (Object-Relational Mapper) which enables us to work with databases using .NET objects.
In this article, we will learn how to perform CRUD (Create, Read, Update and Delete) operations in ASP.net Core MVC, using Entity Framework Core. Here, I have used the "Code First" approach for developing Entity model and as a development tool, I am using "Visual Studio Code". Here, I am doing manual database migration.
To use Entity Framework and MVC, we need to include certain references in project.json file. The following highlighted references need to be included.
{
"version": "1.0.0-*",
"buildOptions": {
"preserveCompilationContext": true,
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
},
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Mvc.Formatters.Xml": "1.0.0",
"Microsoft.Framework.Configuration.Json": "1.0.0-beta7"
},
"imports": [
"dnxcore50",
"portable-net452+win81"
]
}
}
}
I have created “TestTables” table in database. It has three columns - Id, Name, and Description. Id is primary key and auto-incremented.
CREATE TABLE [dbo].[TestTables] (
[Id] INT NOT NULL IDENTITY(1,1),
[Name] VARCHAR(50) NULL,
[Description] VARCHAR(50) NULL,
CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED (
[Id] ASC
) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
) ON [PRIMARY];
The next step is to create Entity model and TestTable entity.
EntityMoidel.cs
using Microsoft.EntityFrameworkCore;
namespace WebApplication
{
public class EntityModelContext : DbContext
{
public DbSet<TestTable> TestTables { get; set; }
}
}
TestTable.cs
namespace WebApplication
{
public class TestTable
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
Now, I am adding "appsettings.json" file which holds various settings for the application. I have added database connection string in this file.
Appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=10.124.130.116;Initial Catalog=TestDB;User ID=sa; Password=Passwd@12"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Startup.cs file is used to register services and injection of modules in HTTP pipeline. It has Startup class which is triggered when application launches the first time.
This is same as Application_Start() event of global.asax file.
In this file, I have registered various services, like MVC, EF etc. Also, configure some global settings, like assigning connection string to dbcontext.
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Framework.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.IO;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace WebApplication
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
var builder = new ConfigurationBuilder(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var configuration = builder.Build();
services.AddMvc();
services.AddEntityFramework()
.AddDbContext<EntityModelContext>(options =>
options.UseSqlServer(configuration["ConnectionStrings:DefaultConnection"]));
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
app.Run(context =>
{
return context.Response.WriteAsync("");
});
}
}
}
Creating Application User Interface
The next step is to create UI. In this step, I have created Controller and various required View. I have “HomeController.cs” file under the Controllers folder and created two Views under the “Views” folder. Following snap is showing the hierarchy of folders and files within our project.

Following is the code snippet for HomeController.





Nilanjan DattaPosted Oct 4, 2018, 8:29 AM
Where is the project.json file?
Luis Alberto Delgado de la FlorPosted Jun 7, 2017, 2:31 PM
Is there a link where I can download this project?
RakeshPosted Nov 21, 2016, 1:16 PM
Good one sir.............