This Months Calendar, Posts for the current week, All Archived Posts

LINQ inheritance in VS2008? sucks

This article is going to be rather different from my regular fair. I'm going to blog about something I've run into that is completely frustrating at work using LINQ.

Family, you're not going to get much out of this post, so proceed at your own risk.

For those who don't know LINQ is Microsoft's version of ActiveRecord?. ActiveRecord? is a great way of working with database information. For example, if I had three tables, Company, Department, and Worker, I could create relationships based on primary and foreign keys between them. If you tell ActiveRecord? of these relationships, you can translate our relational databases into object hierarchies.

To find the department a person works for, I could type "Worker.Department.Name" which would take the worker(which I already retrieved from the database) and get the department's name from the database. All without me needing to make a specific database call or command, it just knows what to retrieve and how to get it.

Likewise I could get a list of all employee names in a department by "Department.Workers.each do |w| names += w.Name end". Many database operations without me specifically calling database code. Very convenient.

LINQ supports this same functionality, in the same way someone with dementia remembers their phone number.

After typing the commands to setup the relationship, you can make use of this power object oriented ability, until you touch the database designer.

The designer is completely ignorant of the ability to link tables in this way. Which is absurd, because it knows about inheriting these classes (more on this later). When you make any change, including something as trivial as dragging a table around a view, it discards any modifications you have made and regenerates everything from scratch. Microsoft's solution to this is to use a partial class to save persistent changes, but this only supports a subset of the attributes needed.

For example, to inherit from a class, you multiple [InheritanceMapping?(...)] attributes, one per class. And on a column, you must include IsDiscriminator?=true in the definition to help LINQ determine which class to sort a row into. The first attribute can be defined in the separate non-generated file, but the second *MUST* be defined in the designer.cs file, which always gets overwritten.

Also, you can use the designer to create Inheritance, but it doesn't insert the required InheritanceMapping? attributes, so the code won't function.

Active Record is so much more flexible. It determines these things at *runtime* so your objects always match the database schema, and you need only maintain the database.

LINQ must have this information at Design time, which means you have the burden of updating the Database AND the project files with any change, and it does so in a broken manner.

The joys of working with Microsoft.

Comments for 30 June 2008

You need to hop on your messenger some time. I have an inquiry for you about a possible contract job
 Posted at 08:30 by Caindris 

« 06/05/2008 | Calendar

Friday, June 13, 2008

Comment
Author


Page last modified June 30, 2008, at 08:30 AM