In the previous article, I have explained how to implement a code first approach when we have an existing database. Now, I will explain how to implement a code first approach when we don’t have an existing database. Let‘s see step by step how to implement it.
See the previous articles for basic details.
- Entity Framework in MVC - Part One
- Entity Framework in MVC - Part Two
- Entity Framework in MVC - Part Three
Step 1
Open Visual Studio >> File >> Project >> Web application >> choose MVC >> OK.
After opening the project, first, we have to create a domain class.
Right-click on Models folder and add a class like below. I am adding an Employee class.
- public class Employee
- {
- public int EmpId { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public string Email { get; set; }
- public string MobileNo { get; set; }
- }
Step 2
Now, I am going to add a DbContext class. For that, right-click the Models folder and add a class and give the name EmpDataContext:DbContext.
- public class EmpDataContext : DbContext
- {
- public DbSet<Employee> employees { get; set; }
- }
Step 3
Now, we have to add a Controller. Go to Controllers folder and add a controller.

When we add a controller, in this controller an Index Action method is automatically created. We can change it to a user-friendly method name. Now, create the object of your Dbcontext class and write the logic for retrieval of the data.

- public class DemoController : Controller
- {
- EmpDataContext objDataContext = new EmpDataContext();
- // GET: Demo
- public ActionResult EmpDetails()
- {
- return View(objDataContext.employees.ToList());
- }
- }
After that, add a View for displaying the employee records. Right-click on action method and add a View.

Step 4
Now, before the retrieval of data, we have to set our connection string in web.confifile.
- <connectionStrings>
- <add name="MySqlConnection" connectionString="Data Source=MANNU;database=MyDemoDB;User Id=sa;Password=123;" providerName="System.Data.SqlClient" />
- </connectionStrings>

Now, set the connection string in DbContext class. If we don’t want to give manual connection string, then when running the project, it will automatically create the connection string with the same name as the class name of DbContext class. If we create manually, then we have to pass our Connection String name in base class parameter.

- public class EmpDataContext : DbContext
- {
- public EmpDataContext()
- : base("name=MySqlConnection")
- {
- }
- public DbSet<Employee> employees { get; set; }
- }
As we know, in this Code First approach when we will run the project, at that moment automatically, it will create the DataBase and the Table; table name will be same as our domain Class name; as below my class name is Employee. If we want to change the table name in a user-friendly name as “ TblEmployee”, then we can use Table class attribute. Also, we can set the Primary key in SQL table from our program by using the Key class attribute in “[ ]” bracteates.
For using this functionality, we have to import the namespace like below.
- using System.ComponentModel.DataAnnotations; (Import namespace)
- using System.ComponentModel.DataAnnotations.Schema;
- [Table("TblEmployee")]
- public class Employee
- {
- [Key]
- public int EmpId { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public string Email { get; set; }
- public string MobileNo { get; set; }
- }
Step 5
Now, we will run the project and then check in SQL.

Step 6
Now, we will complete our CRUD operation using the Code First approach. So for that, we will add one Controller.
First, we will write the code for Create operation. For this, we have to create the action method for getting the request named as Create method.
- public ActionResult create()
- {
- return View();
- }
Now, right-click on Create Action Method and add View.

And, after that, we will write HTML code like below.
- @model Example3.Models.Employee
- @using (Html.BeginForm())
- {
- <div class="form-horizontal">
- <h4>Employee</h4>
- <hr />
- <div class="form-group">
- @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.MobileNo, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.MobileNo, new { htmlAttributes = new { @class = "form-control" } })
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create" class="btn btn-default" />
- </div>
- </div>
- </div>
- }
Now, we will write the code for posting the details.
- [HttpPost]
- public ActionResult create(Employee objEmp)
- {
- objDataContext.employees.Add(objEmp);
- objDataContext.SaveChanges();
- return View();
- }
Now, run the project and insert the data.


After that, click on the Create button. Then we will write the code for retrieving the data.
- EmpDataContext objDataContext = new EmpDataContext();
- // GET: Demo
- public ActionResult EmpDetails()
- {
- return View(objDataContext.employees.ToList());
- }
Here, right click on the Action method “EmpDetails” and press Add View option. And write below html code,








kamal aPosted Jul 20, 2020, 12:51 PM
Very Good Explanation.. its so useful for me...
Shan SundarPosted Dec 3, 2018, 4:05 AM
Very Nice Explanations Im the very beginer of MVC. Now I clear about CRUD operaions. Thank you soo much Mithilesh Kumar.
Farhan AhmedPosted Jul 24, 2018, 3:36 AM
Nice one ....
Yurik UndisputedPosted Jul 19, 2018, 3:20 PM
I looked for simple and easy articles for learning and understanding but your info gave all of i need, thanks a lot.
Hadshana KamalanathanPosted Jul 17, 2018, 5:54 PM
Thank you for sharing.