Saturday, August 16, 2008

[Tutorial] ADO.NET Entity Framework
Part 4 - Creating inherited entities

The last part explained how to trace the automatically generated SQL statements when using the Framework in combination with LINQ. This part explains some basics on creating inherited entities. You need to have the correctly configured project from the last part as prerequisite for the following examples.

  • Let's say that all products that have the Boolean MakeFlag attribute set to true are sold out and must be manufactured. The following example shows how to query these products from the DB.

You will have to write much verification code, if you want to do special operations on products that are sold out. But there is a much more elegant way of solving this task. Using the Entity Framework you may create virtual/inherited entities that only exist in your business layer.

  • Add a new entity from within the model view of the ADO.NET Entity Data Model file ("AdventureWorks.edmx" for the example).

  • Select as base type the entity that you want to inherit from and give the new entity a meaningful name (base type Product and name SoldOutProduct for the example).

  • 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 if it is false then it is a Product for the example)

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


No comments: