Rock.Reflection.GetDbContextForEntityType C# (CSharp) Method

GetDbContextForEntityType() public static method

Gets the appropriate DbContext based on the entity type
public static GetDbContextForEntityType ( Type entityType ) : System.Data.Entity.DbContext
entityType System.Type Type of the Entity.
return System.Data.Entity.DbContext
        public static System.Data.Entity.DbContext GetDbContextForEntityType( Type entityType )
        {
            Type contextType = typeof( Rock.Data.RockContext );
            if ( entityType.Assembly != contextType.Assembly )
            {
                var contextTypeLookup = Reflection.SearchAssembly( entityType.Assembly, typeof( System.Data.Entity.DbContext ) );

                if ( contextTypeLookup.Any() )
                {
                    contextType = contextTypeLookup.First().Value;
                }
            }

            System.Data.Entity.DbContext dbContext = Activator.CreateInstance( contextType ) as System.Data.Entity.DbContext;
            return dbContext;
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Gets the type of the i entity for entity.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="guid">The unique identifier.</param>
        /// <returns></returns>
        public static Rock.Data.IEntity GetIEntityForEntityType(Type entityType, Guid guid)
        {
            var dbContext = Reflection.GetDbContextForEntityType(entityType);

            Rock.Data.IService serviceInstance = Reflection.GetServiceForEntityType(entityType, dbContext);
            if (serviceInstance != null)
            {
                System.Reflection.MethodInfo getMethod = serviceInstance.GetType().GetMethod("Get", new Type[] { typeof(Guid) });
                return(getMethod.Invoke(serviceInstance, new object[] { guid }) as Rock.Data.IEntity);
            }

            return(null);
        }