Entity Framework - Code First approach
Entity Framework introduced Code-First approach from Entity Framework 4.1. Code-First is mainly useful in Domain Driven Design. In Entity Framework Code First approach, you do not have to create the database for your application. You just have to add classes to your application and the database will be generated automatically.
So we first start by writing C# or VB.net classes and context class. When we run the application, Code First APIs will create the new database (if it does not exist yet) and map classes with the database using default code-first conventions. We can also configure your domain classes to override default conventions to map with database tables using DataAnnotation attributes or fluent API.
So let's createa Windows Forms application, then Right click on your project in the solution explorer and select Manage NuGet Packages. Search for EntityFramework and install it as shown below.
So we first start by writing C# or VB.net classes and context class. When we run the application, Code First APIs will create the new database (if it does not exist yet) and map classes with the database using default code-first conventions. We can also configure your domain classes to override default conventions to map with database tables using DataAnnotation attributes or fluent API.
So let's createa Windows Forms application, then Right click on your project in the solution explorer and select Manage NuGet Packages. Search for EntityFramework and install it as shown below.
Add a class to the project and name it 'Employee'
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
Then add context class which should be derived from DbContext and name it ModelContext.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace WindowsFormsApplication8
{
public class ModelContext:DbContext
{
public ModelContext() : base()
{
}
public DbSet<Employee> EmployeeList { get; set; }
}
}
Now, we are done with the required classes for the code-first approach. We will now add Employee using context class as shown below. The code in main method in Program.cs
using(var context=new ModelContext())
{
Employee emp = new Employee() { Name = "aaaaa", Address = "aaaaa" };
context.EmployeeList.Add(emp);
context.SaveChanges();
}
If you run the application, you will see that the application runs successfully and the database is created one Employee is successfully inserted into the database.
Now the question that comes to mind is, where is the data and the database in which we have added some data and then retrieved it from database. By convention, DbContext has created a database for you.
- If a local SQL Express instance is available then Code First has created the database on that instance.
- If SQL Express isn’t available, then Code First will try and use LocalDb.
- The database is named after the fully qualified name of the derived context.
These are just the default conventions and there are various ways to change the database that Code First uses.
For more explanation check out this video:
Entity Framework - Code First approach
Reviewed by Bloggeur DZ
on
10:46
Rating:
Aucun commentaire: