Rock.Data.RockContext.OnModelCreating C# (CSharp) Method

OnModelCreating() protected method

This method is called when the context has been initialized, but before the model has been locked down and used to initialize the context.
protected OnModelCreating ( DbModelBuilder modelBuilder ) : void
modelBuilder DbModelBuilder The builder that defines the model for the context being created.
return void
        protected override void OnModelCreating( DbModelBuilder modelBuilder )
        {
            ContextHelper.AddConfigurations( modelBuilder );

            modelBuilder.Conventions.Add( new GetAddressStoreFunctionInjectionConvention() );
            modelBuilder.Conventions.Add( new GetGeofencingGroupNamesStoreFunctionInjectionConvention() );

            try
            {
                //// dynamically add plugin entities so that queryables can use a mixture of entities from different plugins and core
                //// from http://romiller.com/2012/03/26/dynamically-building-a-model-with-code-first/, but using the new RegisterEntityType in 6.1.3

                // look for IRockEntity classes
                var entityTypeList = Reflection.FindTypes( typeof( Rock.Data.IRockEntity ) )
                    .Where( a => !a.Value.IsAbstract && ( a.Value.GetCustomAttribute<NotMappedAttribute>() == null ) && ( a.Value.GetCustomAttribute<System.Runtime.Serialization.DataContractAttribute>() != null ) )
                    .OrderBy( a => a.Key ).Select( a => a.Value );

                foreach ( var entityType in entityTypeList )
                {
                    modelBuilder.RegisterEntityType( entityType );
                }

                // add configurations that might be in plugin assemblies
                foreach ( var assembly in entityTypeList.Select( a => a.Assembly ).Distinct() )
                {
                    modelBuilder.Configurations.AddFromAssembly( assembly );
                }
            }
            catch ( Exception ex )
            {
                ExceptionLogService.LogException( new Exception( "Exception occurred when adding Plugin Entities to RockContext", ex ), null );
            }
        }