MSDN Magazine - February 2008 - (Page 34) Navigation properties are used to navigate evaluated in determining a product’s specific instance type. Using the Entity Model across associations in the CSDL. The navigation properties become public properties on Designer, I right-click in the designer and their EntityType class and are used to refer choose Add | Entity from the popup menu. I to another entity, or set of entities, associatthen enter DiscontinuedProduct as the new ed with the original entity. For example, the entity name and select Product as the base Product EntityType has a navigation propentity (see Figure 7). erty named Categories, which will reference In the next step, I want to define the difa Category entity for a particular Product enferentiating factor between these entities tity instance. The Category EntityType also using a Condition, so I select the Product has a navigation property named Products. EntityType in the designer and go to the This exists so that with a Category entity Entity Mapping Details window. I then select instance, you can reference its associated the Discontinued property in the Column Product entities. Mappings and remove the mapping. This removes the Discontinued property from The designer can be used to add, edit, or remove entity types, associations, scalar both the Product and DiscontinuedProduct properties, and navigation properties. You Figure 7 Creating an Inherited Entity EntityTypes (When you create a Condition, can, for example, change the names of all of the available expressions for the Condition the EntityTypes to a singular form that is popular among devel- must not already be used as a property). Next, I go to the Maps to opers when naming entities. Simply click the EntityType’s name Products section of the Entity Mapping Details window and add a and edit the name in place. Or you can select the EntityType and Condition of Discontinued = 0. I then select the DiscontinuedProduct change its name in the Properties window. For these examples, I EntityType and add a Condition of Discontinued = 1. renamed all EntityTypes to a singular format, which is reflected Creating derived entities is really quite easy when you use this in Figure 6. After making this change, I also had to fix the XML technique. If you want, you can also add additional properties to the I modified to add the Stored Procedure call as a method of the derived entity beyond its base class’s properties. When you create NWEntities EntityContainer. This is pretty simple, as I just had to an instance of a DiscontinuedProduct in your .NET code and save update the code to refer to the Product EntityType (as opposed to it, the Entity Framework realizes that it must set the Discontinued Products), as shown here: column to a value of 0 in the database because of the Conditions the MSL and simply enforce a filter on the retrieval of rows. When This brings up an important point. It is a good idea to name data is saved, the Conditions are used to determine the value to write your main elements, such as EntityType, EntityContainer, and to the underlying database column based on the derived type. Association, before you start modifying the data model. This will minimize the number of manual changes you’ll need to make, as Wrapping Up well as minimize any code changes you might need to make for Using the design tools in Visual Studio 2008 and the XML files, code you may have already written against the data model. In case you can design an Entity Data Model that uses inheritance, invokes you run into problems, the Error List window is your best friend stored procedures, and models your business instead of writing code as it will very likely show you which elements have invalid refer- directly against your relational database schema. Once a solid enences in the XML. tity model has been designed, interacting with it from APIs, such Finally, while the designer shows the conceptual model, the Entity as Object Services, is quite simple—and modifications to the data Mapping Details window provides a means to view and edit the model will not require changes in the database since the mappings mappings (the MSL) between the EDM and the data store. can insulate the conceptual model from the storage model. If some of the concepts I’ve discussed here regarding the Entity Framework Derived Entities are foreign to you, I highly recommend you check out my overview The tools that help you design your entity model can also help of the Entity Framework, which is available at msdn.microsoft.com/msdn you modify the model. One of the keys to object-oriented pro- mag/issues/07/07/DataPoints. gramming is the concept of inheritance. The EDM supports creating and modifying inherited entities both in the XML and visually Code download available at msdn.microsoft.com/msdnmag/code08.aspx. in the EDM designer. Send your questions and comments for John to mmdata@microsoft.com. To demonstrate the task of designing inherited entities, I will John Papa is a senior .NET Consultant with ASPSOFT (aspsoft.com) and a baseball fanatic who create a DiscontinuedProduct EntityType based on the Product spends most of his summer nights rooting for the Yankees with his family and his faithful dog, Kadi. John, a C# MVP, has authored several books on ADO, XML, and SQL Server. He can often EntityType. The Product EntityType has a Boolean scalar prop- be found speaking at industry conferences, such as VSLive, or blogging at codebetter.com/ erty named Discontinued, which I will use as the Condition to be blogs/john.papa. 34 msdnmagazine Data Points http://msdn.microsoft.com/msdnmag/issues/07/07/DataPoints http://msdn.microsoft.com/msdnmag/issues/07/07/DataPoints http://msdn.microsoft.com/msdnmag/code08.aspx http://aspsoft.com http://codebetter.com/blogs/john.papa
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.