System.Data.Entity.Internal.LazyInternalContext.CreateModel C# (CSharp) Method

CreateModel() public static method

Creates an immutable, cacheable representation of the model defined by this builder. This model can be used to create an ObjectContext or can be passed to a DbContext constructor to create a DbContext for this model.
public static CreateModel ( LazyInternalContext internalContext ) : DbCompiledModel
internalContext LazyInternalContext
return DbCompiledModel
        public static DbCompiledModel CreateModel(LazyInternalContext internalContext)
        {
            var contextType = internalContext.Owner.GetType();

            DbModelStore modelStore = null;
            if (!(internalContext.Owner is HistoryContext))
            {
                modelStore = DbConfiguration.DependencyResolver.GetService<DbModelStore>();
                if (modelStore != null)
                {
                    var compiledModel = modelStore.TryLoad(contextType);
                    if (compiledModel != null)
                    {
                        return compiledModel;
                    }
                }
            }

            var modelBuilder = internalContext.CreateModelBuilder();

            var model
                = (internalContext._modelProviderInfo == null)
                      ? modelBuilder.Build(internalContext._internalConnection.Connection)
                      : modelBuilder.Build(internalContext._modelProviderInfo);

            internalContext._modelBeingInitialized = model;

            if (modelStore != null)
            {
                modelStore.Save(contextType, model);
            }

            return model.Compile();
        }

Usage Example

        public void CreateModel_uses_DbCompiledModel_from_ModelStore_when_available()
        {
            var store = new Mock <DbModelStore>();

            var dbCompiledModelInStore = new DbCompiledModel();

            store.Setup(c => c.TryLoad(It.IsAny <Type>())).Returns(dbCompiledModelInStore);
            store.Setup(c => c.Save(It.IsAny <Type>(), It.IsAny <DbModel>()));

            try
            {
                var dependencyResolver = new SingletonDependencyResolver <DbModelStore>(store.Object);
                MutableResolver.AddResolver <DbModelStore>(dependencyResolver);

                var mockContext = new Mock <LazyInternalContext>(
                    new Mock <DbContext>().Object, new Mock <IInternalConnection>().Object, null, null, null, null, null)
                {
                    CallBase = true
                };

                var model = LazyInternalContext.CreateModel(mockContext.Object);

                Assert.Same(dbCompiledModelInStore, model);

                store.Verify(c => c.TryLoad(It.IsAny <Type>()), Times.Once(),
                             "should load existing model");

                store.Verify(c => c.Save(It.IsAny <Type>(), It.IsAny <DbModel>()), Times.Never(),
                             "should not call Save when loading model from store");
            }
            finally //clean up
            {
                MutableResolver.ClearResolvers();
            }
        }
All Usage Examples Of System.Data.Entity.Internal.LazyInternalContext::CreateModel