##########################################################
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
##########################################################