Tutorial: ADO.NET Entity Framework

This Tutorial provides information on how to configure and use the ADO.NET Entity Framewok in your projects.
Example source code in C# is provided for each of the steps so that you can quickly understand how to get started.

Part 1 - Setting up a project
[Source Code]  [Blog Link]

This first part provides all steps necessary to configure a project for using the ADO.NET Entity Framework.

  • Add a new item of type ADO.NET Entity Data Model to your existing project
  • A wizard will be displayed in which you select to generate from an existing DB 
  • Select your data source for the data connection and select the DB you want to make available
    (select SQL Server and AdventureWorks DB)
  • The necessary files, references and data structures are now created and linked automatically.
    The file "AdventureWorksDesigner.cs" contains the auto-generated classes in this example.
    The App.Config file contains the connection string to the DB.
    The project is now prepared for the use of the Entity Framework!


Part 2 - Accessing data and using LINQ (1/2)
[Source Code]  [Blog Link]

This part shows how to easily query information from the configured data sources.
The example shows how to easily display information from within the DB with very little code.

  • Initialize an object of the auto-generated AdventureWorksEntities class and display some product data

  • Run the application and you will get an output with the product data
  • The following example displays the same results as the last example but this time by using LINQ

  • A good practice is to externalize the creation of the LINQ query for better extensibility, readability and understanding


Part 3 - Accessing data and using LINQ (2/2)
[Source Code]  [Blog Link]

This part explains how to trace the automatically generated SQL statements that are executed in the background.

  • Cast the query object into an "ObjectQuery<Product>" for being able to access the "ToTraceString()" function

  • Run the application and look at the output of the "ToTraceString()" function
  • Now change the LINQ query so that there are additional constraints
  • Run the application again to compare the differences in the generated SQL statements
  • Now add a Lambda expression to see what type of SQL statements are generated in this case
  • Run the application again to compare the differences in the generated SQL statements


Part 4 - Creating inherited entities
[Source Code]  [Blog Link]

This part explains some basics on creating inherited entities.

  • Products that have the MakeFlag attribute set to true are sold out and must be manufactured.
    The following example shows how to query these products from the DB.

  • Add a new entity from within the model view of the ADO.NET Entity Data Model file
  • Select as base type the entity that you want to inherit from and give the new entity a meaningful name

  • The new entity will be created in the model and will be automatically linked to the Product entity.
    Delete the attribute that you want to use as key for distinguishing a SoldOutProduct from a Product.
    Delete the MakeFlag attribute from the Product entity for the example.
  • Add conditions within the mapping details of the SoldOutProduct and Product entities.
    Add a table mapping of SoldOutProduct to Product and add that if MakeFlag is true it is a SoldOutProduct.

 

  • You may now use the new SoldOutProduct entity in your queries to add special operations and treatment

Part 5 - Updating data in the data source [Source Code]  [Blog Link]

In this part you will learn how to update data in the DB.

  • Update the value of the property that you want to change (product standard cost for the example).
    Then call the SaveChanges() method. That's it! You now already have updated the value in the DB.

  • Concurrency checks on properties can be activated.
    Concurrency problems on these properties will then lead to optimistic concurrency exceptions.
  • No more hassle with SQL statements that need to be modified anymore.
    The entity data model just needs to be updated if the data structure has changed.
  • Custom verification logic to assure that updated data is conforming to certain business rules can be applied.
    The auto-generated classes in the data model are already prepared for this purpose.
    They are providing partial classes and partial methods that can be extended.

  • Add a new class to the project (AdventureWorksEntities.cs for the example)
  • Modify the code within the new class to implement the partial class Product and the partial method OnStandardCostChanging(...).
    Then add the constraint that the standard cost cannot be less than 0.

  • Assign a value that is less than 0 in the main function of the project.

  • Run the application and you will get the expected exception.


Find further explications and a more detailed step-by-step guide on my Blog:

http://jasondeoliveira.com

This part shows how to easily query information from the configured data sources. You need to have a console project with an ADO.NET data model that links to the AdventureWorks DB as prerequisite for the following examples.

The first example shows how to easily display information from within the DB with very little code.

Last edited Sep 15, 2010 at 9:51 AM by JasonOliveira, version 15