/// <summary> /// Gets the navigation properties for the entity. /// </summary> /// <typeparam name="Entity">the type of entity</typeparam> /// <param name="context">The context.</param> /// <returns>the list of navigation properties for the entity</returns> public static string[] GetNavigationProperties <Entity>(DbContext context) { string[] properties; Type t = EntityTypeHelper.GetModelType(typeof(Entity)); lock (DictNavProp) { DictNavProp.TryGetValue(t, out properties); if (properties != null) { return(properties); } // Get the System.Data.Entity.Core.Metadata.Edm.EntityType // associated with the entity. ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext; // create method CreateObjectSet with the generic parameter of the base-type MethodInfo method = typeof(ObjectContext).GetMethod("CreateObjectSet", Type.EmptyTypes) .MakeGenericMethod(t); dynamic objectSet = method.Invoke(objectContext, null); IEnumerable <dynamic> keyMembers = objectSet.EntitySet.ElementType.NavigationProperties; properties = keyMembers.Select(k => (string)k.Name).ToArray(); DictNavProp.Add(t, properties); } return(properties); }
/// <summary> /// Gets the properties of the entity. /// </summary> /// <typeparam name="Entity">The type of the ntity.</typeparam> /// <param name="context">The context.</param> /// <param name="dbobj">The dbobj.</param> /// <returns>Returns the list of properties</returns> public static string[] GetProperties <Entity>(DbContext context, Entity dbobj, bool isMock = false) where Entity : class { Type t = EntityTypeHelper.GetModelType(typeof(Entity)); string[] properties; Dict.TryGetValue(t, out properties); if (properties != null) { return(properties); } if (!isMock) { properties = context.Entry(dbobj).CurrentValues.PropertyNames.ToArray(); } else { properties = dbobj.GetType().GetProperties().Select(x => x.Name).ToArray(); } Dict.Add(t, properties); return(properties); }
/// <summary> /// Gets the properties of the entity. /// </summary> /// <typeparam name="Entity">The type of the ntity.</typeparam> /// <param name="context">The context.</param> /// <param name="dbobj">The dbobj.</param> /// <returns>Returns the list of properties</returns> public static string[] GetProperties <Entity>(DbContext context, Entity dbobj, bool isMock = false) where Entity : class { Type t = EntityTypeHelper.GetModelType(typeof(Entity)); string[] properties; lock (Dict) { Dict.TryGetValue(t, out properties); if (properties != null) { return(properties); } properties = dbobj.GetType().GetProperties().Select(x => x.Name).ToArray(); Dict.Add(t, properties); } return(properties); }