Code First is Domain Driven Design.In this approach,
you create classes for your domain entity rather than design your database first.
This means EF API will create database based on your domain classes and configuration.
So you need to start coding first in C# and then EF will create database from your code.
Step by Step way to create CodeFirst approach in ASP.NET MVC OR CONSOLE APPLICATION:-
Step 1:OPEN VISUAL STUDIO EDITOR AND CREATE NEW PROJECT
Step 2:INSTALL ENTITYFRAMEWORK DLL FROM MANAGE NUGET PACKAGES
Step 3: CREATE CONNECTION STRING IN WEB.CONFIG OR APP.CONFIG, DEPENDING ON THE PROJECT CHOSEN BY YOU.
Step 4:Create a new class called SampleDBContext.cs
<connectionString>
<add name="connectsqlserverdb" connectionString="Server=.;Database=employeedb4;uid=sa;password=Slq!123" providerName="System.Data.SqlClient"><add>
<connectionString>
Step 5:Create Customer class
public class SampleDBContext:DbContext
{
public SampleDBContext():base("Name=connectsqlserverdb")
{
}
public DbSet
BEFORE RUNNING CONSOLE APPLICATION,IMAGE OF SQLSERVER DATABASE.
public class Customer
{
[Key]
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public string Address { get; set; }
public string Country { get; set; }
public string City { get; set; }
}
Step 6:NOW USING DBCONTEXT CLASS ADD CUSTOMER IN PROGRAM.CS
Step 7:NOW RUN CONSOLE APPLICATION
static void Main(string[] args)
{
using (SampleDBContext ctx = new SampleDBContext())
{
Customer objcust = new Customer();
objcust.CustomerID = 1;
objcust.CustomerName = "SHIVA";
objcust.Address = "KAILASA";
objcust.Country = "INDIA";
objcust.City = "KASHMIR";
ctx.Customer.Add(objcust);
ctx.SaveChanges();
}
}