1Optimus Confidential 2013
Entity Framework
2Optimus Confidential 2013
• What is Entity Framework
• Creating Entity Model
• Querying the Model
• Examples – Insert, Update, Delete
• Benefits
• Drawbacks
❖ Agenda for today
3Optimus Confidential 2013
❖ ORM’s provides a way to map
ORM is:
• (Wikipedia) A programming(Wikipedia) A programming
technique for converting data between incompatible type
systems(Wikipedia) A programming technique for converting
data between incompatible type systems (such as DBMS) in
relational databases(Wikipedia) A programming technique for
converting data between incompatible type systems (such as
DBMS) in relational databases and object-oriented programming
languages.
•It does the plumbing work for you to aggregate, modify
and save your data back to its storage in Object Oriented
manner (easy to understand, maintain and extend)
4Optimus Confidential 2013
❖ What is Entity Framework
• Microsoft’s newest data access technology
• Visual Studio:
– Examines database
– Creates objects that represent data schema
• LINQ (Language Integrated Query)
– Replaces SQL
– Select, sort, filter, etc.
5Optimus Confidential 2013
❖ Creating Entity Model, step 1
• Create a class library project
• Add new item, select the ADO.NET Entity Data Model
template
6Optimus Confidential 2013
❖ Creating Entity Model, step 2
7Optimus Confidential 2013
❖ Creating Entity Model, step 3
• Adjust the entities, mapping etc.
8Optimus Confidential 2013
❖ Querying the Model
• You can query the model in different ways:
– Using LINQ
– Using Entity SQL with Object Services
9Optimus Confidential 2013
❖ Querying the Model
1.Querying with LINQ
var context = new NWEntities();
var query = from c in context.Customers
select c;
foreach(Customer c in query)
Console.WriteLine(c.CompanyName);
10Optimus Confidential 2013
❖ Querying the Model
2. Entity SQL with Object Services
List<Customer> customers =
Context.Customer.ToList();
foreach (Customer c in customers)
Console.WriteLine(c.CompanyName);
11Optimus Confidential 2013
❖ Examples
• INSERT
Console.WriteLine("Create new order");
var order = new Order { CustomerID = "SEVES",
EmployeeID = 1 };
context.AddToOrders(order);
context.SaveChanges();
12Optimus Confidential 2013
❖ Examples
• Update
Console.WriteLine("Changing order");
var order = from o in context.Orders
where o.OrderID == 11078
select o;
order.Single().OrderDate = DateTime.Now;
context.SaveChanges();
13Optimus Confidential 2013
❖ Examples
• Delete
Console.WriteLine("Deleting order");
var order = from o in context.Orders
where o.OrderID == 11078
select o;
context.DeleteObject(order.Single());
context.SaveChanges();
14Optimus Confidential 2013
❖ Benefits
• Eliminates disconnect between code & database
– No sql statements in code
• Use intellisense to write query
• Automatic table joins
• Full CRUD(create retrieve update delete) functionality
• LINQ works on all data collections
– Arrays, lists, dataviews, etc.
15Optimus Confidential 2013
❖ Drawbacks
• Microsoft proprietary technology
• LINQ query syntax different than SQL
• Synchronization between database and entity objects
– Change database
– Rebuild entity objects

Entity framework amit gupta

  • 1.
  • 2.
    2Optimus Confidential 2013 •What is Entity Framework • Creating Entity Model • Querying the Model • Examples – Insert, Update, Delete • Benefits • Drawbacks ❖ Agenda for today
  • 3.
    3Optimus Confidential 2013 ❖ORM’s provides a way to map ORM is: • (Wikipedia) A programming(Wikipedia) A programming technique for converting data between incompatible type systems(Wikipedia) A programming technique for converting data between incompatible type systems (such as DBMS) in relational databases(Wikipedia) A programming technique for converting data between incompatible type systems (such as DBMS) in relational databases and object-oriented programming languages. •It does the plumbing work for you to aggregate, modify and save your data back to its storage in Object Oriented manner (easy to understand, maintain and extend)
  • 4.
    4Optimus Confidential 2013 ❖What is Entity Framework • Microsoft’s newest data access technology • Visual Studio: – Examines database – Creates objects that represent data schema • LINQ (Language Integrated Query) – Replaces SQL – Select, sort, filter, etc.
  • 5.
    5Optimus Confidential 2013 ❖Creating Entity Model, step 1 • Create a class library project • Add new item, select the ADO.NET Entity Data Model template
  • 6.
    6Optimus Confidential 2013 ❖Creating Entity Model, step 2
  • 7.
    7Optimus Confidential 2013 ❖Creating Entity Model, step 3 • Adjust the entities, mapping etc.
  • 8.
    8Optimus Confidential 2013 ❖Querying the Model • You can query the model in different ways: – Using LINQ – Using Entity SQL with Object Services
  • 9.
    9Optimus Confidential 2013 ❖Querying the Model 1.Querying with LINQ var context = new NWEntities(); var query = from c in context.Customers select c; foreach(Customer c in query) Console.WriteLine(c.CompanyName);
  • 10.
    10Optimus Confidential 2013 ❖Querying the Model 2. Entity SQL with Object Services List<Customer> customers = Context.Customer.ToList(); foreach (Customer c in customers) Console.WriteLine(c.CompanyName);
  • 11.
    11Optimus Confidential 2013 ❖Examples • INSERT Console.WriteLine("Create new order"); var order = new Order { CustomerID = "SEVES", EmployeeID = 1 }; context.AddToOrders(order); context.SaveChanges();
  • 12.
    12Optimus Confidential 2013 ❖Examples • Update Console.WriteLine("Changing order"); var order = from o in context.Orders where o.OrderID == 11078 select o; order.Single().OrderDate = DateTime.Now; context.SaveChanges();
  • 13.
    13Optimus Confidential 2013 ❖Examples • Delete Console.WriteLine("Deleting order"); var order = from o in context.Orders where o.OrderID == 11078 select o; context.DeleteObject(order.Single()); context.SaveChanges();
  • 14.
    14Optimus Confidential 2013 ❖Benefits • Eliminates disconnect between code & database – No sql statements in code • Use intellisense to write query • Automatic table joins • Full CRUD(create retrieve update delete) functionality • LINQ works on all data collections – Arrays, lists, dataviews, etc.
  • 15.
    15Optimus Confidential 2013 ❖Drawbacks • Microsoft proprietary technology • LINQ query syntax different than SQL • Synchronization between database and entity objects – Change database – Rebuild entity objects