MikaelEliasson / EntityFramework.Utilities

Provides extensions for EntityFramework that doesn't exist out of the box like delete and update by query and bulk inserts
443 stars 175 forks source link

Add support to TPT or handle error on EFMapping #103

Open norcino opened 8 years ago

norcino commented 8 years ago

I have an implementation of the TPT pattern using EF. Once added the tables belonging to the TPT the InsertAll stopped working because there is an exception not handled when the EFMapping tries to handle a mapping belonging to the TPT.

 if (mapping.EntityTypeMappings.Any(m => m.IsHierarchyMapping))
 {
    var withConditions = mapping.EntityTypeMappings.Where(m => m.Fragments[0].Conditions.Any()).ToList();
     if (withConditions.Count > 0)
     {
         tableMapping.TPHConfiguration = new TPHConfiguration
         {
             ColumnName = withConditions.First().Fragments[0].Conditions[0].Column.Name,
             Mappings = new Dictionary<Type, string>()
         };

In the code below I show the point where it fails, the snippet shows also how I'm handling the issue, ignore the mapping if the condition withConditions.Count > 0 is false.

At this point in time I don't really need support for TPT, but of course it would make this library more complete. This quick fix in the code for now is doing the job because InsertAll will fail only if I try to add one of the Child Type belonging to the TPT.

For more info on TPT: MSDN - TPT