0% found this document useful (0 votes)
35 views

Entity Framework Notes

The document describes the database first approach in Entity Framework. It involves 1) creating a database table, 2) adding Entity Framework to a .NET project, 3) generating entity classes from the database table, 4) creating an object of the entity class, 5) adding it to the DbSet property, and 6) calling SaveChanges() to persist the changes to the database. Records can also be deleted by getting an entity, removing it from the DbSet, and calling SaveChanges().

Uploaded by

Pankaj Haldikar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Entity Framework Notes

The document describes the database first approach in Entity Framework. It involves 1) creating a database table, 2) adding Entity Framework to a .NET project, 3) generating entity classes from the database table, 4) creating an object of the entity class, 5) adding it to the DbSet property, and 6) calling SaveChanges() to persist the changes to the database. Records can also be deleted by getting an entity, removing it from the DbSet, and calling SaveChanges().

Uploaded by

Pankaj Haldikar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Database first approach

1) Create table in database

create table Employee


(
Eno int primary key,
Ename varchar(50),
Job varchar(50),
Salary money,
Dname varchar(50)

2) Create new c#.net project and install entity framework using Nugate package manager
console by command
Install-package EntityFramework command.
It will add latest entityframework to your project.
3) To project add ADO.NET Entity Data Model through new item and do the settings. It will
create class containing properties based on table fields

a) It contains context.cs file which contains class context derived from DbContext
containing various members along with
public DbSet<Employee> Employees { get; set; }
4) Create object of Dbcontext class
5) Create object of Employee class
6) Add created object to Employees collection of DbContext
7) Call SaveChanges() methods of DbContext class
As shown below
class Program
{
static void Main(string[] args)
{
var context = new CompanyDBEntities();
var employee = new Employee()
{
Eno=2000,
Ename="Mangesh Jambhale",
Salary=5000,
Dname="computer studies",
Job="teaching",
Econtactno="1234567"
};
context.Employees.Add(employee);
context.SaveChanges();
}
}

Delete Record from table


Employee employee = context.Employees.FirstOrDefault(e => e.Ename.Equals("R. T.
Thorat"));
context.Employees.Remove(employee);
context.SaveChanges();

You might also like