Reflection IT LINQ & Entity Framework Fons Sonnemans (Trainer)

Slides:



Advertisements
Similar presentations
The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Advertisements

Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
.NET 3.5 – Mysteries. NetFx Evolution NetFx 1.0 C# 1.0, VB 7.0, VS.NET NetFx 1.1 C# 1.1, VB 7.1, VS 2003 NetFx 2.0 C# 2.0, VB 8.0, VS 2005 NetFx 3.0 C#
© Logica All rights reserved ADO vNext LINQ LINQ to SQL Entity Framework Freek Leemhuis
DEV306 LINQ (Language Integrated Query)
1 Visual Studio 2008 Fons Sonnemans (Trainer) Reflection IT
ASP.NET 3.5 New Features. 2 Agenda What's New in.NET Framework 3.5? Visual Studio 2008 Enhancements LINQ (Language Integrated Query) New ASP.NET Server.
C# 3.0 & LINQ Raimond Brookman – IT Architect
LinqToSharePoint SandBoxed Solution Shakir Majeed Khan
Damien Guard (BSc, MBCS) Guernsey Software Developer Forum Language Integrated Query:
LINQ: Language-Integrated Queries (To be included in C # 3.0) Technology developed by Anders Hejlsberg & friends at Microsoft (2005) Presented by Tal Cohen.
The Microsoft Technical Roadshow 2007 Advances for Data in VS “Orcas” Mike Taulty Developer & Platform Group Microsoft Ltd
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
Discover, Master, InfluenceSlide 1 SQL Server Compact Edition and the Entity Framework Rob Sanders Readify.
Introduction to ADO Entity Framework ir Denis VOITURON Source:
Eric Nelson Developer & Platform Group Microsoft Ltd
ORM Concepts, ADO.NET Entity Framework (EF), ObjectContext Doncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
1 Entity Framework Introduction. Outline Goals of Entity Framework 2.
Slides from Gang Luo, Xuting Zhao and Damien Guard
LINQ Boot Camp ADO.Net Entity Framework Presenter : Date : Mahesh Moily Nov 26, 2009.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
Entity Framework, a quickstart Florin−Tudor Cristea, Microsoft Student Partner.
ADO.NET ENTITY FRAMEWORK Mike Taulty Developer & Platform Group Microsoft UK
Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
Entity Framework Overview. Entity Framework A set of technologies in ADO.NET that support the development of data-oriented software applications A component.
Ventsislav Popov Crossroad Ltd.. 1. ASP.NET Data Source Controls  SqlDataSource  EntityDataSource  ObjectDataSource 2. Entity Data Model and ADO.NET.
Introduction to LINQ Lecture # 19 August Introduction How do you interrogate/manipulate data? What if you could do the work in a type-safe," string-free.
Ronnie Saurenmann Principal Architect Microsoft Switzerland.
LINQ and C# 3.0 Mads Torgersen Program Manager for the C# Language Microsoft Corporation.
LINQ: It’s Not Your Father’s Data Access Denny Boynton Anheuser-Busch Companies.
The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation.
ORM Concepts, ADO.NET Entity Framework (EF), ObjectContext Doncho Minkov Telerik Corporation.
ADO.NET Entity Framework Mike Taulty Developer & Platform Group Microsoft Ltd
Lap Around Visual Studio 2008 &.NET 3.5 Enhancements.
 Language Integrated Query  Make query a part of the language  Component of.NET Framework 3.5  Shipped with Visual Studio 2008.
Reactive Database Access in Scala with Slick 3
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
Language Integrated Query (LINQ). Data Access Programming Challenges Developers must learn data store-specific query syntax Multiple, disparate data stores.
Ventsislav Popov Crossroad Ltd.. 1. ASP.NET Data Source Controls  SqlDataSource  EntityDataSource  ObjectDataSource 2. Entity Data Model and ADO.NET.
By: Luis Carranco CIS764 - Fall  What is LINQ  Architecture  How does it work?  Samples/Demo  Why to use LINQ? 2.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
Applied Linq Putting Linq to work Introducing… Class-A Kennisprovider Microsoft development Training Coaching Alex Thissen Trainer/coach.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
Linq Overview Vincent GERMAIN. Evolution - Rappel Langage  C# 2.0  C# 3.0 (Local type inference, Lambda expression, Method extension,Anonymous type)
Damien Guard (BSc, MBCS) Guernsey Software Developer Forum Language Integrated Query:
Ken Casada Developer Evangelist Microsoft Switzerland
ADO.NET 3.0 – Entity Data Model Gert E.R. Drapers Chief Software Architect Visual Studio Team Edition for Database Professionals Microsoft Corporation.
Language Integrated Query Mike Taulty Developer & Platform Group Microsoft Ltd
2 Behind every great site, there is great data Eric Nelson Developer Evangelist Microsoft UK
Introduction to Entity Framework
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2016
LINQ for SQL SQL Saturday May 2009 David Fekke.
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2013
Intro to LINQ Part 2 – Concepts and LINQ to Objects
Language Integrated Query: (LINQ) An introduction
LiNQ SQL Saturday David Fekke.
Entity Framework By: Casey Griffin.
9/20/2018 2:13 PM Visual Studio развитие технологий доступа к данным на платформе Microsoft.NET Роман Здебский Эксперт по технологиям разработки.
ADO.NET Entity Framework
ADO.NEXT Advances in Data Access for 2008
An Introduction to Entity Framework
Language Integrated Query (LINQ)
MSDN Roadshow Rerun Welcome.
Технологии доступа к данным на платформе Microsoft.NET
ADO.NET Entity Framework
LINQ - 2 Ravi Kumar C++/C# Team.
Presentation transcript:

Reflection IT LINQ & Entity Framework Fons Sonnemans (Trainer)

2 Agenda C# 3.0 LINQ LINQ to SQL Entity Framework

3 ADO.NET Problems using (SqlConnection c = new SqlConnection( … )) { c.Open(); string sql ="SELECT c.Name, c.Phone, c.CreationDate " + "FROM Customers c " + "WHERE SqlCommand cmd = new SqlCommand(sql, c); "London"); SqlDataReader dr = c.ExecuteReader(cmd); while(dr.Read()) { string name = dr.GetString(0); string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2); … } Queries in quotes Loosely bound arguments Loosely typed resultsets No compiletime checking No IntelliSense

Reflection IT C# 3.0

5 C# 3.0: Local Variable Type Inference Local variable type inference is a feature in C# 3.0 where you can use the var keyword instead of explicitly specifying the type of a variable. The C# 3.0 compiler makes the type of the variable match the type of the right side of the assignment. public void Foo() { var i = 5; var s = "Hello"; var d = 1.0; var z;// compiler error, no initializer z = DateTime.Today; }

6 C# 3.0: Object Initializers public class Point { private int x, y; public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } } Point a = new Point { X = 0, Y = 1 }; Point a = new Point(); a.X = 0; a.Y = 1; Field or property assignments

7 C# 3.0: Anonymous Types Different anonymous object initializers that define properties with same names in the same order generate the same anonymous type var emp = new { Name = "Fons", Salary = 2000, DateTime.Today.Year }; var year = emp.Year; class XXX { public string Name { get; set; } public int Salary { get; set; } public int Year { get; set; } }

8 C# 3.0: Extension Methods Extend existing types with additional methods. namespace MyStuff { public static class Util { public static bool IsWeekend(this DateTime value) { return (value.DayOfWeek == DayOfWeek.Sunday || value.DayOfWeek == DayOfWeek.Saturday); } using MyStuff; Brings extensions into scope dt.IsWeekend()  DateTime.IsWeekend(dt) DateTime dt = DateTime.Today; bool b = dt.IsWeekend();

9 C# 3.0: Lambda Expressions delegate string SomeDelegate(string s); private static string TestMethod1(string s) { return s.ToUpper(); } … SomeDelegate d1 = new SomeDelegate(TestMethod1); string a = d1("abcde"); SomeDelegate d3 = delegate(string s) { return s.ToUpper(); }; string a = d3("abcde"); SomeDelegate d4 = s => s.ToUpper(); string a = d4("abcde"); C# 1.0C# 2.0C# 3.0 SomeDelegate d2 = TestMethod1; string a = d2("abcde"); C# 2.0 Delegate Inference Anonymous Method Lambda Expression OO Function- Pointer

Reflection IT LINQ Language-Integrated Query

11 What is LINQ? Uniform way to write queries over data  Data == Objects  Imperative  Declarative  Works against objects, relational and XML LINQ is about query keywords  Built into new languages C# 3.0 and VB 9.0 LINQ is about query operators  40+ standard query operators are defined  Methods that operate in queries or act on its results

12 C# 3.0: Query Expressions from id in source { from id in source | join id in source on expr equals expr [ into id ] | let id = expr | where condition | orderby ordering, ordering, … } select expr | group expr by key [ into id query ] Starts with from Zero or more from, join, let, where, or orderby Ends with select or group by Optional into continuation

13 from c in customers where c.State == "WA" select new { c.Name, c.Phone }; customers.Where(c => c.State == "WA").Select(c => new { c.Name, c.Phone }); C# 3.0: Query Expressions Queries translate to method invocations  Where, Join, OrderBy, Select, GroupBy, …

14 Language-Integrated Query Others… VB C# LINQ Provider ADO.NET Based LINQ To Datasets LINQ To SQL LINQ To Entities LINQ To XML LINQ To Objects Objects XML Relational

15 Two kinds of LINQ Enumerable typesQueryable types ExecutionLocal in-memoryUsually remote ImplementationIterators using yield return Expression tree parsing Interface IEnumerable IQueryable ProvidersLINQ to ObjectsLINQ to SQL LINQ to Entities Other APIsLINQ to XML LINQ to DataSets

16 Standard Query Operators RestrictionWhere ProjectionSelect, SelectMany OrderingOrderBy, OrderByDescending, ThenBy, ThenByDecending GroupingGroupBy QuantifiersAny, All, Contains PartitioningTake, Skip, TakeWhile, SkipWhile SetsDistinct, Union, Concat, Intersect, Except ElementsFirst, FirstOrDefault, Last, Single, ElementAt AggregationCount, Sum, Min, Max, Average ConversionToArray, ToList, ToDictionary, ToLookup CastingCast, OfType

17 LINQ to Objects

18 LINQ to SQL O/R Mapper  Maps.NET classes to relational SQL Server data Translates LINQ queries to SQL execution Supports change tracking for insert, update, delete operations Built on top of ADO.NET and integrates with connection-pooling and transactions

19 LINQ to SQL from c in db.Customers where c.City == "London" select c.CompanyName IQueryable<T> SELECT CompanyName FROM Customer WHERE City = 'London' SQL Query or SProc Resultset Objects db.Customers.InsertOnSubmit(c1); c2.City = "Asten"; db.Customers.DeleteOnSubmit(c3); SubmitChanges() INSERT INTO Customer … UPDATE Customer … DELETE FROM Customer … DML or SProcs [Attributes] Application SQL Server LINQ to SQL (DataContext) LINQ to SQL (DataContext)

20 LINQ to SQL

Reflection IT ADO.NET Entity Framework Included in Visual Studio SP1

22 ADO.NET Entity Framework O/R Mapper  Maps.NET classes to relational SQL data Translates LINQ and Entity SQL queries to SQL execution Supports change tracking for insert, update, delete operations Built on top of ADO.NET and integrates with connection-pooling and transactions

23 ADO.NET Entity Framework RDBMS Datastore Objects Schema Datastore Objects Schema Entity Data Model Schema Entity Data Model Schema Conceptual ModelStorage/Logical Model *.CSDL *.MSL *.SSDL Map ObjectConnection ObjectCommand ObjectDataReader ObjectConnection ObjectCommand ObjectDataReader EntityClient (ADO.NET API ) SQL Server Providers Oracle DB2 Object Services (ORM API) ObjectContext EntityObject ObjectContext EntityObject ObjectQueries Entity SQL DataReaderLINQ ObjectsEntity SQL Objects Entity Data Model  Abstraction over a relational database  Consists of conceptual & logical models  Provides mapping layer between conceptual model and logical model Entity Client  Provides classes similar to ADO.NET Providers  Can return results as DbDataReaders Entity SQL  Textual SQL language for dynamic queries Object Services  Enables you to work with object instances  Provides persistence and change tracking LINQ to Entities  Provides LINQ syntax and strongly- typed objects for queries over EDM Entity Data Model

24 Entity Data Model The edmx file is composed of three important parts: The csdl section which describes your entities The ssdl section which describes your database The msl section which do the mapping between the two others RDBMS (tables, views, SP’s, FN’s) RDBMS (tables, views, SP’s, FN’s) OO Classes (Properties + Methods) OO Classes (Properties + Methods) Datastore Objects Schema Datastore Objects Schema Entity Data Model Schema Entity Data Model Schema Conceptual ModelStorage/Logical Model *.CSD L *.MSL *.SSD L Map

25 Mapping Examples Store Customer CustomerId First Last EntitiesMapping Customers ID FirstName LastName IsPremium Overdraft AccountManager PremiumCustomer Overdraft AccountManager ? ?

26 Mapping Examples Store Good Customers ID FirstName LastName Bad Customers ID ForeName Surname Customers CustomerId First Last Type Entities Mapping Type=“G” Type=“B”

27 LINQ to Entity Framework from c in db.Customers where c.City == "London" select c.CompanyName IQueryable<T> SELECT CompanyName FROM Customer WHERE City = 'London' SQL Query or SProc Resultset Objects db.AddToCustomer(c1); c2.City = "Asten"; db.DeleteObject(c3); SaveChanges() INSERT INTO Customer … UPDATE Customer … DELETE FROM Customer … DML or SProcs.edmx File (Models & Mapping) Application RDBMS LINQ to EF (ObjectContext) LINQ to EF (ObjectContext)

28 Entity SQL (SELECT only) // Object Services using (NorthwindEntities db = new NorthwindEntities()) { // Entity SQL var q = db.CreateQuery ("SELECT VALUE p FROM NorthwindEntities.Products AS p " + "WHERE p.UnitPrice new ObjectParameter("price", 60)); foreach (var prod in q) { Console.WriteLine(prod.ProductName); } // Entity Client using (EntityConnection con = new EntityConnection("name=NorthwindEntities")) { con.Open(); // Entity SQL EntityCommand cmd = new EntityCommand("SELECT p.ProductName FROM NorthwindEntities.Products" + " AS p WHERE p.UnitPrice con); cmd.Parameters.AddWithValue("price", 60); using (EntityDataReader r = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) { while (r.Read()) { Console.WriteLine(r["ProductName"]); }

29 EF Providers in Progress VendorDB Support MicrosoftSQL Server Core LabOracle, MySQL, PostgreSQL, SQLite IBMDB2, Informix Dynamic Server MySQL ABMySQL NpgsqlPostgreSQL OpenLinkMany via OpenLink ODBC or JDBC PhoenixSQLite DataDirectOracle, Sybase, SQL Server, DB2 Firebird

30 LINQ to SQL vs Entity Framework LINQ to SQLADO.NET Entities Framework Database SupportSQL ServerMany Object Relational Mapping Capabilities Simple -> 1:1Complex MetadataAttributesedmx file StatusReleasedBeta, Summer 2008

31 ADO.NET Entity Framework

32 Summary LINQ is a really important technology Native query integration within languages improves productivity and error checking LINQ to SQL and ADO.NET Entity Framework are both O/R Mappers Using LINQ in ASP.NET is both easy and fun

33 Resources us/netframework/aa aspx us/netframework/aa aspx Visual Studio 2008 Upgrade Training 

34 Questions