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?
EF Core allows to map two or more entities to a single row. This is called table splitting or table sharing.
Inheritance in the Entity Framework is similar to inheritance for classes in C#. In Entity Framework, you can map an inheritance hierarchy to single or multiple database tables based on your requirements. In this article, you will learn how to map your inheritance hierarchy to database tables in SQL Server.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With