0% found this document useful (0 votes)
76 views2 pages

Core MVC EF

1. Create an ASP.NET Core MVC application and add Entity Framework Core packages. 2. Create a DbContext class that inherits from DbContext and defines a DbSet for the Employee entity. 3. Configure the DbContext to use SQL Server by specifying the connection string in code and appsettings.json.

Uploaded by

anigaf ridwan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views2 pages

Core MVC EF

1. Create an ASP.NET Core MVC application and add Entity Framework Core packages. 2. Create a DbContext class that inherits from DbContext and defines a DbSet for the Employee entity. 3. Configure the DbContext to use SQL Server by specifying the connection string in code and appsettings.json.

Uploaded by

anigaf ridwan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

##########################################################

Create database and table from visual studio


##########################################################
1. Create new Application asp.net core mvc

2. adding components
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools

3. Create new folder and named it Data

4. create class file in the Data Folder and named it with MVCDemoDataContext

5. Adding library
using ASPNETMVC.Models.Domain;
using Microsoft.EntityFrameworkCore;

and create constructor

using ASPNETMVC.Models.Domain;
using Microsoft.EntityFrameworkCore;

namespace ASPNETMVC.Data
{
public class MVCDemoDbContext : DbContext
{
public MVCDemoDbContext(DbContextOptions options) : base(options)
{
}

public DbSet<Employee> Employees { get; set; }


}
}

6. Create folder Domain inside folder models and add new class with name
Employee.cs, and create property

namespace ASPNETMVC.Models.Domain
{
public class Employee
{
public Guid Id { get; set; }

public string Name { get; set; }

public string Email { get; set; }

public long Salary { get; set; }

public DateTime DateOfBirth { get; set; }

public string Department { get; set;}


}
}

7. Add new connection


going to Program.cs and edit or add new builder services for connection
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<MVCDemoDbContext>(options =>

options.UseSqlServer(builder.Configuration.GetConnectionString("MvcDemoConnectionSt
ring")));

8. adding detail connection in appsettings.json


{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MvcDemoConnectionString":
"server=lieandryridwan;database=MVCDemoDb;Trusted_connection=true;TrustServerCertif
icate=True"
}
}

##########################################################
Create insert page
##########################################################

You might also like