Click here to Skip to main content
15,886,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a large application using EF 6 and need a way to process calls to SaveChanges() in a generic fashion. I have Googled this repeatedly and found only outdated materials. I have a partial class setup that intercepts the SaveChanges call just fine. The current error I am receiving occurs here:
C#
public static string[] GetKeyNames<T>(DbContext context) where T : class
   {
       Type t = typeof(T);
       ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
       MethodInfo method = typeof (ObjectContext).GetMethod("CreateObjectSet",
                 Type.EmptyTypes).MakeGenericMethod(t);

      ->  dynamic objectSet = method.Invoke(objectContext, null);

       IEnumerable<dynamic> keyMembers = objectSet.EntitySet.ElementType.KeyMembers;

       string[] keyNames = keyMembers.Select(k => (string)k.Name).ToArray();
       return keyNames;
   }


The method.Invoke is causing "Mapping and metadata information could not be found for EntityType 'System.Data.Entity.DbContext'.".

I tried Googling the error and found nothing that applied to EF 6.1.1. I've been fighting with this for 3 days now. Any help would be appreciated. Does anyone have a complete example using generics and reflection to override the SaveChanges method?

Thanks,

Bill
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900