Your SlideShare is downloading. ×
0
MySQL, LINQ and the ADO.NET Entity Framework <ul><li>Reggie Burnett, MySQL </li></ul><ul><li>David Sceppa, Microsoft </li>...
Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to acce...
Working with DataSets <ul><li>Basic example </li></ul>//Execute a query, retrieve and store the results connectionString =...
Working with DataSets <ul><li>Benefit </li></ul><ul><ul><li>Rapid development - Designers, wizards, bound controls make it...
Moving to data access layers <ul><li>Focus on your application model </li></ul><ul><ul><li>Queries expressed in terms of a...
Moving to data access layers <ul><li>Basic example </li></ul>//Partial class to separate data access code public class  Cu...
Moving to data access layers <ul><li>Benefit </li></ul><ul><ul><li>Division of labor - Manage data access code separately,...
Introducing LINQ <ul><li>What is LINQ? </li></ul><ul><ul><li>Stands for Language INtegrated Query </li></ul></ul><ul><ul><...
Introducing LINQ <ul><li>LINQ Example - Querying an array </li></ul>//Create an array of integers int [] myarray =  new   ...
LINQ-enabled classes <ul><li>Collection classes extended </li></ul><ul><ul><li>Arrays, Hashtable, List<T>, etc.  </li></ul...
Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to acce...
LINQ to DataSet <ul><li>DataSet class enhanced to support LINQ </li></ul><ul><ul><li>Available via typed and untyped DataS...
LINQ to DataSet <ul><li>Benefits </li></ul><ul><ul><li>Quickest way to use LINQ w/ .NET 2.0 feature set </li></ul></ul><ul...
LINQ to SQL <ul><li>Benefits </li></ul><ul><ul><li>Rapid application development scenarios </li></ul></ul><ul><ul><li>Quer...
Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to acce...
<ul><li>What is LINQ to Entities? </li></ul><ul><ul><li>LINQ implementation of the Entity Data Model </li></ul></ul>LINQ t...
LINQ to DataSet <ul><li>DataSet class enhanced to support LINQ </li></ul><ul><ul><li>Available via typed and untyped DataS...
LINQ to Entities or LINQ to SQL? <ul><li>LINQ to SQL </li></ul><ul><ul><li>Shipped with Visual Studio 2008 and .NET 3.5 </...
LINQ to Entities: Generating Queries <ul><li>Converting LINQ queries to SQL </li></ul><ul><ul><li>Compiler converts code i...
Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to acce...
Entity Framework: LINQ and more <ul><li>ADO.NET Entity Framework Layers </li></ul><ul><ul><li>First implementation of  Ent...
Entity Framework Query Options <ul><li>LINQ to Entities </li></ul><ul><li>Sweet spot: </li></ul><ul><li>Strongly typed que...
Entity Framework Query Options <ul><li>ObjectQuery<T> / ObjectQuery(Of T) </li></ul><ul><li>Sweet spot: </li></ul><ul><li>...
Entity Framework Query Options <ul><li>EntityClient Provider </li></ul><ul><li>Sweet spot: </li></ul><ul><ul><li>Untyped q...
Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to acce...
ADO.NET Data Services <ul><li>Formerly known as &quot;Project Astoria&quot; </li></ul><ul><li>Part of ASP.NET Extensions P...
ADO.NET Data Services <ul><li>Service exposed via lightweight data access API </li></ul><ul><li>Supply both location and q...
Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to acce...
Summary - LINQ <ul><li>Represents a revolution for developers </li></ul><ul><li>Query your objects using SQL-like syntax <...
Summary - ADO.NET Entity Framework <ul><li>More than just LINQ to Entities </li></ul><ul><li>Also supports text-based quer...
Summary - ADO.NET Data Services <ul><li>Designed for Silverlight, AJAX environments </li></ul><ul><li>Lightweight API, con...
Questions?
Upcoming SlideShare
Loading in...5
×

B_110500002

874

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
874
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
50
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "B_110500002"

  1. 1. MySQL, LINQ and the ADO.NET Entity Framework <ul><li>Reggie Burnett, MySQL </li></ul><ul><li>David Sceppa, Microsoft </li></ul>
  2. 2. Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to access your data </li></ul><ul><li>The ADO.NET Entity Framework </li></ul><ul><li>Introducing ADO.NET Data Services </li></ul><ul><li>Summary </li></ul>
  3. 3. Working with DataSets <ul><li>Basic example </li></ul>//Execute a query, retrieve and store the results connectionString = &quot;Data Source=localhost;User ID=...;&quot; ; commandText = &quot;SELECT CompanyName, City FROM Customers &quot; + &quot;WHERE Country = ?Country&quot; ; adapter = new MySqlDataAdapter (commandText, connectionString); adapter.SelectCommand.Parameters.AddWithValue( &quot;?Country&quot;, &quot;Brazil&quot;); table = new DataTable (); adapter.Fill(table); //Display the results DataGridView resultsGrid = new DataGridView (); resultsGrid.DataSource = table; //Submit pending changes commandBuilder = new MySqlCommandBuilder (adapter); adapter.Update(table);
  4. 4. Working with DataSets <ul><li>Benefit </li></ul><ul><ul><li>Rapid development - Designers, wizards, bound controls make it easy to write code and generate user interface </li></ul></ul><ul><li>Drawbacks </li></ul><ul><ul><li>Developers must become SQL experts to build advanced queries </li></ul></ul><ul><ul><li>Code becomes more complex when app model and database schema diverge </li></ul></ul>
  5. 5. Moving to data access layers <ul><li>Focus on your application model </li></ul><ul><ul><li>Queries expressed in terms of app model </li></ul></ul><ul><ul><li>Classes track their own changes </li></ul></ul><ul><li>Data access layer responsible for </li></ul><ul><ul><li>Translating into SQL queries </li></ul></ul><ul><ul><li>Returning results as objects </li></ul></ul><ul><ul><li>Submitting pending changes </li></ul></ul>
  6. 6. Moving to data access layers <ul><li>Basic example </li></ul>//Partial class to separate data access code public class Customer { //Add methods to return data public static List < Customer > GetCustomers ( string country) { List < Customer > results = new List < Customer > (); MySqlConnection connection = new MySqlConnection (connectString); connection.Open(); MySqlCommand command = new MySqlCommand (queryString, connection); command.Parameters.AddWithValue( &quot;?Country&quot; , country); MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) results.Add( Customer .CreateFromReader(reader)); return results; } //Support tracking and submitting changes private string origCustomerId, origCompanyName, ...; public void SubmitChanges() { ... } }
  7. 7. Moving to data access layers <ul><li>Benefit </li></ul><ul><ul><li>Division of labor - Manage data access code separately, only subset of developers need to become SQL gurus </li></ul></ul><ul><li>Drawbacks </li></ul><ul><ul><li>Data access code quickly gets complex </li></ul></ul><ul><ul><ul><li>Classes that span multiple tables </li></ul></ul></ul><ul><ul><ul><li>Handling inheritance hierarchies </li></ul></ul></ul><ul><ul><ul><li>Note: Patterns and frameworks emerge </li></ul></ul></ul>
  8. 8. Introducing LINQ <ul><li>What is LINQ? </li></ul><ul><ul><li>Stands for Language INtegrated Query </li></ul></ul><ul><ul><li>Allows developers to query data structures using SQL-like syntax from within their application’s code </li></ul></ul><ul><ul><li>Is available to both C# and Visual Basic developers in Visual Studio 2008 </li></ul></ul>
  9. 9. Introducing LINQ <ul><li>LINQ Example - Querying an array </li></ul>//Create an array of integers int [] myarray = new int [] { 49, 28, 20, 15, 25, 23, 24, 10, 7, 34 }; //Create a query for odd numbers var oddNumbers = from i in myarray where i % 2 == 1 select i; //Display the results of the query foreach ( int i in oddNumbers) Console .WriteLine(i); //Create a query for odd numbers, sorted var oddNumbers = from i in myarray where i % 2 == 1 orderby i select i; //Create a query for odd numbers, sorted in descending order var oddNumbers = from i in myarray where i % 2 == 1 orderby i descending select i; //Create a query for odd numbers var oddNumbers = from i in myarray where i % 2 == 1 select i; //Compose the original query to create a query for odd numbers var sorted = from i in oddNumbers orderby i descending select i;
  10. 10. LINQ-enabled classes <ul><li>Collection classes extended </li></ul><ul><ul><li>Arrays, Hashtable, List<T>, etc. </li></ul></ul><ul><ul><li>LINQ to DataSet </li></ul></ul><ul><ul><li>Extended for untyped and strongly typed </li></ul></ul><ul><ul><li>LINQ to XML </li></ul></ul><ul><ul><li>Data access technologies </li></ul></ul><ul><ul><li>LINQ to SQL </li></ul></ul><ul><ul><li>LINQ to Entities </li></ul></ul>
  11. 11. Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to access your data </li></ul><ul><li>The ADO.NET Entity Framework </li></ul><ul><li>Introducing ADO.NET Data Services </li></ul><ul><li>Summary </li></ul>
  12. 12. LINQ to DataSet <ul><li>DataSet class enhanced to support LINQ </li></ul><ul><ul><li>Available via typed and untyped DataSet </li></ul></ul>Dim ordersQuery = From o In dataSet.Orders _ Where o.CustomerID = &quot;ALFKI&quot; _ Select o For Each o As NorthwindDataSet.OrdersRow In ordersQuery Console.WriteLine( &quot;{0} {1,10:d}&quot; , _ o.OrderID, o.OrderDate) Next o
  13. 13. LINQ to DataSet <ul><li>Benefits </li></ul><ul><ul><li>Quickest way to use LINQ w/ .NET 2.0 feature set </li></ul></ul><ul><ul><li>Supports query capabilities previously unavailable using a DataSet </li></ul></ul><ul><ul><li>Returns results as series of DataRows or as anonymous types </li></ul></ul><ul><li>Limitations </li></ul><ul><ul><li>All client-side processing </li></ul></ul>
  14. 14. LINQ to SQL <ul><li>Benefits </li></ul><ul><ul><li>Rapid application development scenarios </li></ul></ul><ul><ul><li>Query processing performed at the server </li></ul></ul><ul><ul><li>Designer makes mapping classes to tables simple </li></ul></ul><ul><li>Limitations </li></ul><ul><ul><li>Supports limited mapping scenarios </li></ul></ul><ul><li>Benefits </li></ul><ul><ul><li>Rapid application development scenarios </li></ul></ul><ul><ul><li>Query processing performed at the server </li></ul></ul><ul><ul><li>Designer makes mapping classes to tables simple </li></ul></ul><ul><li>Limitations </li></ul><ul><ul><li>Supports limited mapping scenarios </li></ul></ul><ul><ul><li>No provider model for extensibility </li></ul></ul>
  15. 15. Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to access your data </li></ul><ul><li>The ADO.NET Entity Framework </li></ul><ul><li>Introducing ADO.NET Data Services </li></ul><ul><li>Summary </li></ul>
  16. 16. <ul><li>What is LINQ to Entities? </li></ul><ul><ul><li>LINQ implementation of the Entity Data Model </li></ul></ul>LINQ to Entities <ul><li>What is LINQ to Entities? </li></ul><ul><ul><li>LINQ implementation of the Entity Data Model </li></ul></ul><ul><li>What is the Entity Data Model? </li></ul><ul><ul><li>Definition for your application model </li></ul></ul><ul><ul><li>Map between app model, database schema </li></ul></ul><ul><ul><li>Advanced mapping scenarios supported </li></ul></ul><ul><ul><li>One entity mapped across multiple tables </li></ul></ul><ul><ul><li>Multiple inheritance hierarchy mappings </li></ul></ul><ul><ul><li>Many-to-many without &quot;link&quot; table in model </li></ul></ul>
  17. 17. LINQ to DataSet <ul><li>DataSet class enhanced to support LINQ </li></ul><ul><ul><li>Available via typed and untyped DataSet </li></ul></ul>Dim ordersQuery = From o In dataSet.Orders _ Where o.CustomerID = &quot;ALFKI&quot; _ Select o For Each o As NorthwindDataSet.OrdersRow In ordersQuery Console.WriteLine( &quot;{0} {1,10:d}&quot; , _ o.OrderID, o.OrderDate) Next o
  18. 18. LINQ to Entities or LINQ to SQL? <ul><li>LINQ to SQL </li></ul><ul><ul><li>Shipped with Visual Studio 2008 and .NET 3.5 </li></ul></ul><ul><ul><li>Emphasis on rapid application development </li></ul></ul><ul><ul><li>Supports Microsoft SQL Server family of databases </li></ul></ul><ul><li>LINQ to Entities </li></ul><ul><ul><li>Will ship as an update to .NET 3.5 </li></ul></ul><ul><ul><li>Offers a provider model for third-party databases </li></ul></ul><ul><ul><li>Designed for enterprise-grade data scenarios </li></ul></ul><ul><ul><li>Higher level of abstraction for programming databases </li></ul></ul><ul><ul><li>Just one layer of the overall ADO.NET Entity Framework </li></ul></ul>
  19. 19. LINQ to Entities: Generating Queries <ul><li>Converting LINQ queries to SQL </li></ul><ul><ul><li>Compiler converts code into a LINQ expression tree </li></ul></ul><ul><ul><li>LINQ to Entities converts LINQ expression tree into a DbCommandTree based on mapping information </li></ul></ul><ul><ul><li>DbCommandTree expressed in terms of the database schema </li></ul></ul><ul><ul><li>ADO.NET provider generates a DbCommand </li></ul></ul><ul><ul><li>LINQ to Entities executes the DbCommand, assembles results into the structure(s) specified in the LINQ query </li></ul></ul>
  20. 20. Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to access your data </li></ul><ul><li>The ADO.NET Entity Framework </li></ul><ul><li>Introducing ADO.NET Data Services </li></ul><ul><li>Summary </li></ul>
  21. 21. Entity Framework: LINQ and more <ul><li>ADO.NET Entity Framework Layers </li></ul><ul><ul><li>First implementation of Entity Data Model (EDM) </li></ul></ul><ul><ul><li>Includes a text-based query query language (Entity SQL) </li></ul></ul><ul><ul><li>New Provider – EntityClient </li></ul></ul><ul><ul><li>New ORM stack (Object Services) leverages Entity Client </li></ul></ul><ul><ul><li>LINQ support over Entities </li></ul></ul><ul><ul><li>An evolution of ADO.NET preserving customer investment </li></ul></ul>ADO.NET Provider EntityClient ObjectServices LINQ to Entities
  22. 22. Entity Framework Query Options <ul><li>LINQ to Entities </li></ul><ul><li>Sweet spot: </li></ul><ul><li>Strongly typed queries and results </li></ul><ul><li>Compile-time type checking and Intellisense </li></ul><ul><li>Results returned as objects: entities / projections </li></ul><ul><li>Queries must be well defined at compile time </li></ul>var ordersQuery = from o in context.Orders where o.Customers.CustomerID == &quot;ALFKI&quot; select o; foreach ( var o in ordersQuery) Console .WriteLine( &quot;{0} {1:d}&quot; , o.OrderID, o.OrderDate);
  23. 23. Entity Framework Query Options <ul><li>ObjectQuery<T> / ObjectQuery(Of T) </li></ul><ul><li>Sweet spot: </li></ul><ul><li>Strongly typed results, text-based queries </li></ul><ul><li>Results returned as objects: entities / projections </li></ul><ul><li>Queries more loosely defined at compile-time </li></ul>Dim eql As String = &quot;SELECT VALUE o FROM Orders AS o &quot; & _ &quot; WHERE o.Customer.CustomerID = 'ALFKI'&quot; Dim ordersQuery = context.CreateQuery( Of Order)(esql) For Each o As Order In ordersQuery Console.WriteLine( &quot;{0} {1,10:d} &quot; , o.OrderID, o.OrderDate) Next o
  24. 24. Entity Framework Query Options <ul><li>EntityClient Provider </li></ul><ul><li>Sweet spot: </li></ul><ul><ul><li>Untyped queries and results </li></ul></ul><ul><ul><li>Results returned as DataReaders </li></ul></ul><ul><ul><li>Queries can be generated purely at run time </li></ul></ul><ul><ul><li>No application model classes needed at compile time </li></ul></ul>string eSql = &quot;SELECT VALUE o FROM NorthwindEntities.Orders AS o &quot; + &quot;WHERE o.Customers.CustomerID = 'ALFKI'&quot; ; EntityCommand cmd = new EntityCommand (eSql, connectionString); EntityDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) Console .WriteLine( &quot;{0} {1:d}&quot; , rdr[ &quot;OrderID&quot; ], rdr[ &quot;OrderDate&quot; ]);
  25. 25. Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to access your data </li></ul><ul><li>The ADO.NET Entity Framework </li></ul><ul><li>Introducing ADO.NET Data Services </li></ul><ul><li>Summary </li></ul>
  26. 26. ADO.NET Data Services <ul><li>Formerly known as &quot;Project Astoria&quot; </li></ul><ul><li>Part of ASP.NET Extensions Preview </li></ul><ul><li>Integrated into .NET Framework going forward </li></ul><ul><li>Targeting Web development technologies </li></ul><ul><li>Silverlight and AJAX </li></ul><ul><li>Data returned via Web-friendly formats </li></ul><ul><li>ATOM (XML-based) and JSON </li></ul>
  27. 27. ADO.NET Data Services <ul><li>Service exposed via lightweight data access API </li></ul><ul><li>Supply both location and query as a URI: </li></ul><ul><li>http://myserver/data.svc/Customers[ALFKI]/Orders </li></ul><ul><li>Query converted to LINQ inside of service </li></ul><ul><li>Supports submitting changes </li></ul><ul><li>Specialized Entity Framework logic for submitting changes </li></ul><ul><li>Online data hosting service available </li></ul><ul><li>For more information: </li></ul><ul><li>http://astoria.mslivelabs.com </li></ul>
  28. 28. Agenda <ul><li>From DataSets to data access layers </li></ul><ul><li>Introducing LINQ </li></ul><ul><li>Using LINQ to access your data </li></ul><ul><li>The ADO.NET Entity Framework </li></ul><ul><li>Introducing ADO.NET Data Services </li></ul><ul><li>Summary </li></ul>
  29. 29. Summary - LINQ <ul><li>Represents a revolution for developers </li></ul><ul><li>Query your objects using SQL-like syntax </li></ul><ul><li>LINQ to DataSet </li></ul><ul><li>Rich query scenarios for data residing in a DataSet </li></ul><ul><li>LINQ to Entities </li></ul><ul><li>Provider model for working with other data stores </li></ul><ul><li>Supports enterprise mapping scenarios </li></ul>
  30. 30. Summary - ADO.NET Entity Framework <ul><li>More than just LINQ to Entities </li></ul><ul><li>Also supports text-based query language – ESQL </li></ul><ul><li>ObjectQuery<T> for strongly typed results </li></ul><ul><li>EntityClient for reader-based results </li></ul><ul><li>Enterprise-grade data scenarios </li></ul><ul><li>Provider model for working with other data stores </li></ul>
  31. 31. Summary - ADO.NET Data Services <ul><li>Designed for Silverlight, AJAX environments </li></ul><ul><li>Lightweight API, connect and query via URI </li></ul><ul><li>Retrieve results via familiar formats </li></ul><ul><li>ATOM </li></ul><ul><li>JSON </li></ul>
  32. 32. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×