Polymorphic Entity Framework Designer Example


Url: http://wilderminds.blob.core.windows.net/downloads/testef.zip

I've been digging into the latest version of the Entity Framework Beta (and designer CTP) that dropped a few days ago. I've concocted a small example that shows the derivation model in the Entity Framework. Essentially it is a small model that has a Product type and two types that derive from that: ActiveProduct and DiscontinuedProduct:

Figure 1

Figure 1: Simple Model

The new design tools are pretty basic but interesting nonetheless. They seem to be modeled after the LINQ designer (which doesn't make me excited).  The designer has a surface to draw types (or they can be created from database metadata) and create relationships between types (as seen in figure 1 above).  In addition, there are two information panes: Entity Model Browser and Entity Mapping Details:

Figure 2

Figure 2: The complete Designer Experience (Click image for full size)

The idea of this simple model is to show how you can specify derived types in the designer. The derivation in our case evolves around the Discontinued field in the Products table in the Northwind database. The way that different types are typed in the model is using Conditions.  In our case we are typing the database data as a Product if the Discontinued field is NULL (which should never happen really, but the designer doesn't seem to support the notion of an abstract type yet); if the Discontinued field is true (or 1) then its a DiscontinuedProduct type; and if the Discontinued  field is false (or 0) then its an ActiveProduct.  You can see these mapped in the Entity Mapping Details below:

Figure 3

Figure 3: ActiveProduct Mapping

Figure 4

Figure 4: DiscontinuedProduct Mapping

Once we have the model defined, we can use Object Services to query our data based on type (I could have used LINQ but I think the Entity SQL is clearer in this case):

-- Returns all Products
SELECT VALUE p 
  FROM NWEntities.Products as p

-- Returns only DiscontinuedProducts
SELECT VALUE p 
  FROM NWEntities.Products as p 
 WHERE p IS OF(NWModel.DiscontinuedProduct)

-- Returns only ActiveProducts
SELECT VALUE p 
  FROM NWEntities.Products as p 
 WHERE p IS OF(NWModel.ActiveProduct)

Feel free to download the demo (it only works if you have Orcas Beta 2 and the Entity Framework bits installed) and let me know what you think.



Shawn
Shawn Wildermuth
Author, Teacher, and Coach




My Courses

Wilder Minds Training
Vue.js by Example (New Lower Price)
Bootstrap 4 by Example (New Lower Price)
Intro to Font Awesome 5 (Free Course)
Pluralsight
Building an API with ASP.NET Core (New Course)
Building a Web App with ASP.NET Core, MVC6, EF Core, Bootstrap and Angular (updated for 2.2)
Less: Getting Started (New)
Using Visual Studio Code for ASP.NET Core Projects
Implementing ASP.NET Web API

Application Name WilderBlog Environment Name Production
Application Ver v4.0.30319 Runtime Framework x86
App Path D:\home\site\wwwroot\ Runtime Version .NET Core 4.6.27514.02
Operating System Microsoft Windows 10.0.14393 Runtime Arch X86