The document discusses Entity Framework and LINQ. It defines Entity Framework as an object-relational mapping framework that allows automated data access from domain objects to a relational database. It describes LINQ as a set of features that adds powerful query capabilities to the C# language syntax. The presentation covers Entity Framework architecture and concepts like code first, database first and model first approaches. It also explains LINQ fundamentals like deferred execution, standard query operators, and differences between LINQ and traditional SQL queries.
5. What is Entity Framework?
• Entity framework is an Object/Relational
Mapping (O/RM) framework
• Automated mechanism for accessing &
storing the data in the database
6. What is ORM?
• ORM is a tool for storing data from domain
objects to relational database (like MS SQL
Server)
• O/RM includes three main parts: Domain
class objects, Relational database objects and
Mapping information
7. Why ORM?
• ORM allows us to keep our database design
separate from our domain class design
• This makes the application maintainable and
extendable
8. ORM vs Traditional Data Access
Techniques
• ORM often reduces the amount of code that
needs to be written
• High level of abstraction obscuring what is
actually happening in the implementation
code
11. EF 6 Tools for VS 2012 & 2013
• http://www.microsoft.com/en-
us/download/confirmation.aspx?id=40762
12. Database first
• An existing database can be used
• Code can be auto-generated.
• Extensible using partial classes/ T4 templates
• The developer can update the database
manually
• There is a very good designer, which sync with
the underlining database
13. Code first
• There is full control of the model from the
Code; no EDMX/designer
• No manual intervention to DB is required
• The database is used for data only
• POCO Class
14. Model first
• Good support with EDMX designer
• We can visually create the database model
• EF generates the Code and database script
• Extensible through partial classes
• We can modify the model and update the
generated database.
15. Code first from existing database
• POCO Classes from existing database
18. • Language-Integrated Query (LINQ)
• Set of features that extends powerful query
capabilities to the language syntax of C# and
Visual Basic.
LINQ
19. Three parts
• All LINQ query operations consist of three
distinct actions:
– Obtain the data source.
– Create the query.
– Execute the query.
20. Three parts
• // 1. Data source.
• int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
• // 2. Query creation.
• // numQuery is an IEnumerable<int>
• var numQuery =
• from num in numbers
• where (num % 2) == 0
• select num;
• // 3. Query execution.
• foreach (int num in numQuery)
• {
• Console.Write("{0,1} ", num);
• }
21. The Data Source
• Types that support IEnumerable<T> or a
derived interface such as the generic
IQueryable<T> are called queryable types
22. The Query
• The query specifies what information to
retrieve from the data source or sources
• Query also specifies how that information
should be sorted, grouped, and shaped
before it is returned
• The query expression contains three
clauses: from, where and select
23. Deferred execution
• query variable itself only stores the query
commands
• The actual execution of the query is deferred
until you iterate over the query variable in
a foreach statement
24. Forcing Immediate Execution
• Examples of such queries
are Count, Max, Average, and First. These
execute without an explicit foreach statement
because the query itself must use foreach in
order to return a result.
• That these types of queries return a single value,
not an IEnumerable collection.
• ToList<T>, ToArray<T>
25. LINQ vs SQL
SELECT UPPER(Name)
FROM Customer
WHERE Name LIKE 'A%'
ORDER BY Name
26. LINQ vs SQL
SELECT UPPER(Name) FROM
(
SELECT *, RN = row_number()
OVER (ORDER BY Name)
FROM Customer
WHERE Name LIKE 'A%'
) A
WHERE RN BETWEEN 21 AND 30
ORDER BY Name
27. LINQ vs SQL
var query =
from c in db.Customers
where c.Name.StartsWith ("A")
orderby c.Name
select c.Name.ToUpper();
var thirdPage = query.Skip(20).Take(10);
28. LINQ vs SQL
from p in db.Purchases
where p.Customer.Address.State == "WA" ||
p.Customer == null
where p.PurchaseItems.Sum (pi =>
pi.SaleAmount) > 1000
select p
29. LINQ vs SQL
SELECT p.*
FROM Purchase p
LEFT OUTER JOIN
Customer c INNER JOIN Address a ON c.AddressID = a.ID
ON p.CustomerID = c.ID
WHERE
(a.State = 'WA' || p.CustomerID IS NULL)
AND p.ID in
(
SELECT PurchaseID FROM PurchaseItem
GROUP BY PurchaseID HAVING SUM (SaleAmount) > 1000
)
30. When not to use LINQ for querying databases
• Hand-tweaked queries (especially with
optimization or locking hints)
• Queries that involve selecting into temporary
tables, then querying those tables
31. Query syntax vs Method syntax
• IEnumerable<int>
numQuery1 =
• from num in
numbers
• where num %
2 == 0
• orderby num
• select num;
• IEnumerable<int>
numQuery2 =
numbers.Where(num =>
num % 2 ==
0).OrderBy(n => n);
EDM (Entity Data Model): EDM consist three main parts- Conceptual model, Mapping and Storage model.
Conceptual Model: The conceptual model contains the model classes and their relationships. This will be independent from your database table design.
Storage Model: Storage model is the database design model which includes tables, views, stored procedures and their relationships and keys.
Mapping: Mapping consist information about how the conceptual model is mapped to storage model.
LINQ to Entities:LINQ to Entities is a query language used to write queries against the object model. It returns entities, which are defined in the conceptual model. You can use your LINQ skills here.
Entity SQL: Entity SQL is another query language just like LINQ to Entities. However, it is a little more difficult than L2E and also the developer will need to learn it separately.
Object Service:Object service is a main entry point for accessing data from the database and to return it back. Object service is responsible for materialization, which is process of converting data returned from entity client data provider (next layer) to an entity object structure.
Entity Client Data Provider:The main responsibility of this layer is to convert L2E or Entity SQL queries into a SQL query which is understood by the underlying database. It communicates with the ADO.Net data provider which in turn sends or retrieves data from database.
ADO.Net Data Provider:This layer communicates with database using standard ADO.Net.
Student – ima jednu adresu
One to many – profesor i knjiga
Many to many – student kolegij