LINQ and ADO.NET Entity Framework


ADO.NET Entity Framework Object Services, the upcoming version of ADO.NET includes a layer that can expose database data as regular .NET objects. Furthermore, ADO.NET tools will generate .NET classes that represent the EDM schema in the .NET environment. This makes the object layer an ideal target for LINQ support, allowing developers to formulate queries against a database right from the programming language used to build the business logic. This capability is known as LINQ to Entities.


For example, we discussed this code fragment that would query for objects in a database:

 









using(AdventureWorksDB aw = new

AdventureWorksDB(Settings.Default.AdventureWorks)) {

    Query<SalesPerson> newSalesPeople = aw.GetQuery<SalesPerson>(

        "SELECT VALUE sp " +

        "FROM AdventureWorks.AdventureWorksDB.SalesPeople AS sp " +

        "WHERE sp.HireDate > @date",

        new QueryParameter("@date", hireDate));


    foreach(SalesPerson p in newSalesPeople) {

        Console.WriteLine("{0}\t{1}", p.FirstName, p.LastName);

    }

}




By leveraging the types that were automatically generated by the code-gen tool, plus the LINQ support in ADO.NET, we can re-write the this as:

using(AdventureWorksDB aw = new

AdventureWorksDB(Settings.Default.AdventureWorks)) {

    var newSalesPeople = from p in aw.SalesPeople

                         where p.HireDate > hireDate

                         select p;


    foreach(SalesPerson p in newSalesPeople) {

        Console.WriteLine("{0}\t{1}", p.FirstName, p.LastName);

    }

}





This query written using LINQ will be processed by the compiler, which means that you'll get compile-time validation as the rest of the application code would. Syntax errors as well as errors in member names and data types will be cached by the compiler and reported at compile time instead of the usual run-time errors that are commonplace during development using SQL and a host programming language.

The results of these queries are still objects that represent ADO.NET entities, so you can manipulate and update them using the same means that are available when using Entity SQL for query formulation.

                    

Aspnet Related Tutorials

...more

New Aspnet Resources

...more

Copyright © 2012 VisualBuilder. All rights reserved