Delay loading expensive fields in Entity Framework

2019-05-03 20:50发布

问题:

After hunting around the net, and thinking I'd come up with the solution, I seem to have hit another brick wall.

I have a table in my database: Photos; containing columns for PhotoID, Caption, Ordering, and four sets of binary data: Original, Large, Medium and Small (yes, it was based on the old ASP.NET starter kit with various fixes, etc).

I'm in the process of moving from L2S to Entity Framework for some of the advantages I appear to get with that - so I no longer need to go through a PhotosTags property to get the list of Tags attached to a photo for example, but I'm looking for a way to delay loading the binary data - most of the time, I only need caption, tag, and ID, and I then hand these off to another area to get the binary data when a user actually views the image.

I've taken a look at the following posts:

  1. How to split a data table?
  2. “Table Splitting”: Mapping multiple entity types to the same table.
  3. Chapter 7 of: Entity Framework Learning Guide (7.1 Delay loading of expensive fields)

And I ended up with an Entity Mapping looking like this:

And, as per the links above, I modified the edmx file to include the following Referential Constraint:

<ReferentialConstraint>
  <Principal Role="Photos">
    <PropertyRef Name="PhotoID" />
  </Principal>
  <Dependent Role="PhotoDetails">
    <PropertyRef Name="PhotoID" />
  </Dependent>
</ReferentialConstraint>

The model validates, but doesn't build - because of an issue with the Tags mapping I think:

Error 3019: Problem in Mapping Fragments starting at lines 871, 892: Incorrect mapping of composite key columns. Foreign key constraint 'FK_siteContent_TagsPhotos_siteContent_Photos' from table siteContent_TagsPhotos (PhotoID) to table siteContent_Photos (PhotoID): Columns (PhotoID) in table siteContent_TagsPhotos are mapped to properties (PhotoID) in siteContent_TagsPhotos and columns (PhotoID) in table siteContent_Photos are mapped to properties (PhotoID) in PhotosPhotoDetails. The order of the columns through the mappings is not preserved.

The relationship from Photos to Tags is through a link table, columns PhotoID, TagID.

Has anyone managed to get either those recommendations, or something similar, to work with the Entity Framework in .NET 3.5 SP1 with a data structure like this? Or can you point out the limitations of my GoogleFU?

I know that this will all be much easier in .NET 4, but that's not here, and it will probably be a while after it's released that my host starts offering to install it somewhere.

Thanks.

回答1:

I've hit the same issue and while I haven't found a solution for 3.5SP1 I did confirm that this error goes away with EF 4.0.

Actually, a potential workaround for you would be to use a different field for the primary key in your PhotoDetails entity.