Table-splitting of an inherited entity type?

entity-framework-4

Solution

You can consider this a limitation of Entity Framework: there's actually two pieces, the Model of the underlying database item, and the Entity that is build from Model. The subclass of the Entity doesn't affect the Model whatsoever.

If you don't want to load all of the data for a Model row, project it:

var results = from product in myDB.Products
              where product.Id == productId
              select New 
              {
                  Id = product.Id, 
                  Name = product.Name, 
                  ImageUrl = product.ImageUrl
              };

The SQL query that Entity Framework creates only selects the columns in the `select` clause of the LINQ query.

This also keeps Entity Framework from storing the table row into the `ObjectContext` (or `DBContext` for EF 5) object.

As a side note, I'd be personally tempted to store largish binary data in a NoSQL solution, and just maintain the associated key in the SQL database myself.

Problem

I have an entity type called Image that inherits from Publication (there are 5 other types of Publications, all share 10 common properties). Unfortunately, The Image table in my DB includes 4 binary columns with the data for 4 versions of the image at different resolutions, so there are 4 properties of the EF Image type: BinOriginal, BinHiRes, BinLowRes, BinThumbnail, that contain very large amounts of data. This is affecting performance. I don't want to grab all of the binary data when I'm just generating a series of image links, for example. So I've tried table-splitting, placing the 4 binary fields into a new ImageFile entity a la: http://blogs.msdn.com/b/adonet/archive/2008/12/05/table-splitting-mapping-multiple-entity-types-to-the-same-table.aspx I've ensured the correct table-mapping, added the 1-1 association and included the referential constraint, but I'm getting this error: ``` Error 3033: Problem in mapping fragments starting at line 2731:EntitySets 'ImageFiles' and 'Publications' are both mapped to table 'Images'. Their primary keys may collide. ``` ... it seems there's a problem in that the table being split is involved in an inheritance relationship. I've tried inheriting the new ImageFile EF type from Publication, but then I get an error: ``` Problem in mapping fragments starting at lines 2332, 2374:Two entities with different keys are mapped to the same row ``` QUESTION Is there any way around this, or does the fact that I need the Image EF type to inherit from Publication preclude splitting off the other fields into a new type?

Original source