The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,502,635
since: 19 Jan 2005

ADO.NET Entity Framework - Entity Inheritance Woes

posted Tue 10 Oct 06

In case you've been stuck under a rock for the past couple of months, the ADO.NET Entity Framework is essentially the conglomeration of bits and pieces of disbanded groups within Microsoft, including a lot of the work that was going into the database access model for WinFS, a now-defunct technology that may never see the light of day. What it basically does is build on top of the work done for C# vNext (3.5??) which includes LINQ, Language Integrated Query. LINQ lets you write C# code that looks like this:

var myDataQuery = from Customer in theDatabase.Customers where Customer.FirstName == "Kevin";

How cool is that?!? The Entity Framework takes that one step further by adding a layer of abstraction on top of the relational database called an Entity Model. This Entity Model allows you to create a model that much more elegantly fits into the Object-Oriented Design world.. you can create entities that consist of columns retrieved from multiple tables, you can create entities that inherit from other entities, etc. So far I've been really impressed, but I have one major nitpick.

At the moment, You cannot use a foreign key column as a discriminator column for the purposes of determining entity inheritance. That may sound like a lot, so let's take a look at an example.

Say you have a database with two tables: VideoGames, and VideoGameTypes. It should be obvious that the next thing I'm going to say is in the VideoGames table there is a GameType column that has a foreign key relationship and constraint connecting it to the VideoGameTypes table, probably on the ID column. Also, there's a nullable column called WiimoteOptimized that indicates if a Wii game is optimized for Wii controllers or if its just an ordinary controller game. Because I know that entity inheritance works on discriminator columns, one would think that I should be able to define an entity type called VideoGames which includes all rows in the VideoGames table, and I should also be able to define an entity type called WiiGames, with a condition contained in the underlying mapping file that indicates that in order to qualify for membership in the WiiGames entity set, the entity must have a value of 7 in the column GameType.

This then enables a query like this:

var wiiGames = from GenericGame in theDatabase.VideoGames orderby
GenericGame.Title where GenericGame is WiiGame;

If you don't think that the query above is unbelievably cool, then...well, there's probably no hope for you :)

Here's the rub: This flat out won't work. What will happen is you'll compile , everything will be fantastic, and then you'll run it and get a runtime error that mentions something cryptic about having conflicting conditions on the GameType column. What's going on is that there's a NOT NULL condition placed on the GameType column by the foreign key constraint, and there's a "Must be 7" condition placed on the same column by the WiiGame entity set mapping definition. The problem is that these things conflict and you cannot place an entity inheritance condition on a column that is also a foreign key.

That bites. Hard.

The official solution, according to Atul Adya (MSFT) over at the MSDN forum for ADO.NET vNext, is to create a second column that mirrors the data in the foreign key column. I tried creating a second entity column that mapped to the same database column and I still get the same runtime error.

Correct me if I'm wrong, but at what point did we slip away from reality and decide that we should fix a limitation in the Entity Framework by violating normalization rules in my back-end database?!?!?! p.s. A similar limitation exists in Rails/ActiveRecord as well - so I am really interested in hearing a good fix for this.

If anybody else has a better workaround than the official solution from Microsoft, I would love to hear it, because personally I think the idea of creating two columns in the database : 1 for discrimination and 1 for foreign key establishment is ludicrous.

tags:              

links: digg this    del.icio.us    technorati    reddit




1. kobot left...
Tue 10 Oct 06 10:46 am

well, EVERYBODY would like to hear a good solution for this, probably including even the ADO.NET developers


2. Craig left...
Tue 10 Oct 06 11:45 pm

what about creating the discrimination column on the fly during the select based off the FK - probably too internal for the EF.


3. Kevin Hoffman left...
Wed 11 Oct 06 6:32 am

Its a good thought.. but as far as I can tell, I don't have control over the select that generates the model.. it works in the other direction. I define the model, then I define the mapping, and the schema to which the model maps...and through some "magic" , the mapping provider then creates SQL statements for me.


Tag Related Posts

CLINQ v1.1.0.0 Released!

Fri 02 May 08 5:38 P GMT-05
tags:          

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

Unexpected and Unsafe behavior in LINQ

Wed 14 Nov 07 8:09 P GMT-05
tags:    

Continuous LINQ - Can I write games with it?

Mon 13 Aug 07 3:09 P GMT-05
tags:        

Continuous LINQ

Mon 06 Aug 07 1:21 P GMT-05
tags:    

Dynamic, Observable LINQ Views

Tue 31 Jul 07 1:21 A GMT-05
tags:        

Orcas EDM Wizard Patched

Fri 27 Apr 07 11:56 A GMT-05
tags:      

Installing Orcas Beta 1 - VMware Style

Mon 23 Apr 07 12:16 P GMT-05

Visual Studio "Orcas" - March CTP is Available

Wed 28 Feb 07 12:28 P GMT-05
tags:            

Objective-C Categories vs C# 3.5 Language Extensions

Mon 26 Feb 07 1:05 P GMT-05
tags:                

ASP.NET vs Ruby on Rails : Round 2 (Agility)

Thu 05 Oct 06 11:02 A GMT-05
tags:                      

ASP.NET vs Ruby on Rails : Round 1

Wed 04 Oct 06 1:37 P GMT-05
tags:                

ADO.NET Entity Framework Announced Today!

Wed 16 Aug 06 11:08 A GMT-05

DLinq vs the ADO.NET Entity Framework

Fri 23 Jun 06 4:01 P GMT-05

WPF/XAML and LINQ - A match made in heaven

Tue 06 Jun 06 11:32 A GMT-05
tags:                  

Language Extensions in C# 3.0

Wed 31 May 06 2:17 P GMT-05

Lambda Lambda Lambda

Sun 21 May 06 1:01 A GMT-05

The Adventures of LINQ (Not Zelda)

Fri 19 May 06 11:21 P GMT-05
tags: