ASP.NET Core Application With Code-First Entity Framework Core
By: Parismita Panda
Tags: Entity Framework Core, Aspnetcore, Code First, Entity Framework

Entity Framework Core uses a provider model to access many different databases. It includes providers as NuGet packages which you need to install. Database Providers and the Nuget packages are:

SQL Server : Microsoft.EntityFrameworkCore.SqlServer

MySQL : MySql.Data.EntityFrameworkCore

PostgreSQL : Npgsql.EntityFrameworkCore.PostgreSQL

SQLite : Microsoft.EntityFrameworkCore.SQLite

SQL Compact : EntityFrameworkCore.SqlServerCompact40

In-memory : Microsoft.EntityFrameworkCore.InMemory

EF Core mainly targets the code-first approach and provides little support for the database-first approach because the visual designer or wizard for the DB model is not supported as of EF Core 2.0.

In the code-first approach, EF Core creates the database and tables using migration based on the conventions and configuration provided in your domain classes. This approach is useful in Domain-Driven Design (DDD).

In the database-first approach, EF Core creates the domain and context classes based on your existing database using EF Core commands. This has limited support in EF Core as it does not support visual designer or wizard.

Follow the step by step procedure for implementing Code-First in ASP.NET Core Application.

Create a Database in SQL Server. Here I created the “DB_Profile” database. Here we only create a database we will not add tables. For adding tables we will use code-first in ASP.NET Core Web Application.

Create ASP.NET Core Application Project.

Open visual studio 2019 and select Create a new project.

Then select ASP.NET Core Web Application. Click on the Next button.

Enter your project Name. Click on the Create button.

Create a new ASP.NET Core Web Application will open. Here I select MVC Application(Model-View-Controller). Click on the Create button.

Then your project will open.

Now we will start the code-first entity framework setting. Create a model class “Country.cs” file inside the Model. This model class is representing the table structure. Follow the code:

using System;

using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;

using System.ComponentModel.DataAnnotations.Schema;

using System.Linq;

using System.Threading.Tasks;

namespace CodeFirstApp.Models

{

[Table(name:"Country")]

public class Country

{

[Key]

[Column(TypeName ="bigint")]

public long Id { get; set; }

[Column(TypeName ="varchar(50)")]

public string Name { get; set; }

}

}

Create a context class “ApplicationDbContext.cs” file inside the Model. Context class gathers details about the application’s entity types and how they map to a database schema. This process can be automatic as long as the tool can easily create the DbContext in such a way that it will be configured similarly to how it would be configured at run-time. Follow the code:

using Microsoft.EntityFrameworkCore;

using System.Collections.Generic;

using System.Threading.Tasks;

namespace CodeFirstApp.Models

{

public class ApplicationDbContext:DbContext

{

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){

optionsBuilder.EnableSensitiveDataLogging();

}

public DbSet<Country> countries { get; set; }

public DbSet<State> states { get; set; }

}

}

For more details you can visit my blog…

Originally published at https://c-sharpcodedestination.blogspot.com.