public void CreateInstance_should_return_valid_instance_when_context_constructible_via_factory()
        {
            var contextInfo = new DbContextInfo(typeof(ContextWithoutDefaultCtor));

            Assert.True(contextInfo.IsConstructible);
            Assert.Same(typeof(ContextWithoutDefaultCtor), contextInfo.CreateInstance().GetType());
        }
        public void CreateInstance_should_return_null_when_context_not_constructible()
        {
            var contextInfo = new DbContextInfo(typeof(DbContext));

            Assert.False(contextInfo.IsConstructible);
            Assert.Null(contextInfo.CreateInstance());
        }
        public void CreateInstance_should_return_valid_instance_when_context_constructible()
        {
            var contextInfo = new DbContextInfo(typeof(SimpleContext));

            Assert.True(contextInfo.IsConstructible);
            Assert.Same(typeof(SimpleContext), contextInfo.CreateInstance().GetType());
        }
        public void Can_use_custom_on_model_creating_action_to_configure_model_builder()
        {
            var contextInfo = new DbContextInfo(typeof(ContextWithExternalOnModelCreating2));

            var objectContext = ((IObjectContextAdapter)contextInfo.CreateInstance()).ObjectContext;

            Assert.NotNull(objectContext);
            Assert.False(objectContext.CreateDatabaseScript().Contains("EdmMetadata"));
        }
        // TODO: [Fact(Skip = "SDE Merge - No partial trust yet")]
        public void DbContextInfo_works_under_partial_trust()
        {
            var contextInfo = new DbContextInfo(typeof(AdvancedPatternsMasterContext),
                                                ProviderRegistry.Sql2008_ProviderInfo);

            var context = contextInfo.CreateInstance();

            Assert.NotNull(context);
        }
        public void CreateInstance_should_attach_on_model_creating_custom_action_and_invoke_once()
        {
            var calledCount = 0;

            var contextInfo = new DbContextInfo(typeof(ContextWithExternalOnModelCreating1));

            contextInfo.OnModelCreating = _ => calledCount++;

            contextInfo.CreateInstance();

            Assert.Equal(0, calledCount);

            var objectContext = ((IObjectContextAdapter)contextInfo.CreateInstance()).ObjectContext;

            Assert.NotNull(objectContext);
            Assert.Equal(1, calledCount);

            objectContext = ((IObjectContextAdapter)contextInfo.CreateInstance()).ObjectContext;

            Assert.NotNull(objectContext);
            Assert.Equal(1, calledCount);
        }
        public void CreateInstance_should_use_passed_provider_info_when_building_model()
        {
            var contextInfo = new DbContextInfo(typeof(SimpleContext), ProviderRegistry.SqlCe4_ProviderInfo);

            Assert.Equal(ProviderRegistry.SqlCe4_ProviderInfo.ProviderInvariantName, contextInfo.ConnectionProviderName);
            Assert.Equal(string.Empty, contextInfo.ConnectionString);

            Database.SetInitializer <SimpleContext>(null);

            var objectContext = ((IObjectContextAdapter)contextInfo.CreateInstance()).ObjectContext;

            Assert.NotNull(objectContext);
            Assert.Equal("SqlCeConnection", ((EntityConnection)objectContext.Connection).StoreConnection.GetType().Name);
        }
        public void Supplied_config_used_to_load_original_and_overriden_connection()
        {
            var connection  = new DbConnectionInfo("GetMeFromSuppliedConfig");
            var contextInfo = new DbContextInfo(
                typeof(ContextWithConnectionNameNotInAppConfigFile),
                CreateEmptyConfig()
                .AddConnectionString("GetMeFromSuppliedConfig", "Database=ConnectionFromSuppliedConfig", "System.Data.SqlClient")
                .AddConnectionString("WontFindMeInDefaultConfig", "Database=WontFindMeInDefaultConfig", "System.Data.SqlClient"),
                connection);

            using (var context = contextInfo.CreateInstance())
            {
                Assert.Equal("ConnectionFromSuppliedConfig", context.Database.Connection.Database);
            }
        }
        public void Can_set_hard_coded_connection_from_default_config()
        {
            var connection  = new DbConnectionInfo("OverrideConnectionTest");
            var contextInfo = new DbContextInfo(typeof(SimpleContext), connection);

            Assert.Equal(DbConnectionStringOrigin.DbContextInfo, contextInfo.ConnectionStringOrigin);
            Assert.Equal("System.Data.SqlClient", contextInfo.ConnectionProviderName);
            Assert.Equal("OverrideConnectionTest", contextInfo.ConnectionStringName);
            Assert.True(contextInfo.IsConstructible);

            using (var context = contextInfo.CreateInstance())
            {
                Assert.Equal("ConnectionFromAppConfig", context.Database.Connection.Database);
            }
        }
        public void Can_set_hard_coded_connection()
        {
            var connection  = new DbConnectionInfo("Database=UseThisDatabaseInstead", "System.Data.SqlClient");
            var contextInfo = new DbContextInfo(typeof(SimpleContext), connection);

            Assert.Equal(DbConnectionStringOrigin.DbContextInfo, contextInfo.ConnectionStringOrigin);
            Assert.Equal("System.Data.SqlClient", contextInfo.ConnectionProviderName);
            Assert.Equal(null, contextInfo.ConnectionStringName);
            Assert.True(contextInfo.IsConstructible);

            using (var context = contextInfo.CreateInstance())
            {
                Assert.Equal("UseThisDatabaseInstead", context.Database.Connection.Database);
            }
        }
示例#11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DbMigrator"/> class.
        /// </summary>
        /// <param name="configuration">Configuration to be used for the migration process.</param>
		public DbMigrator(DbMigrationsConfiguration configuration)
			: base(configuration)
		{
			_usersContextInfo = configuration.TargetDatabase == null ?
				new DbContextInfo(configuration.ContextType) :
				new DbContextInfo(configuration.ContextType, configuration.TargetDatabase);
			if (_usersContextInfo.IsConstructible)
			{
				using (var context = _usersContextInfo.CreateInstance())
				{
					DbMigrationContext.Current.DatabaseName = context.Database.Connection.Database;
				}
			}

		}
        public void Can_unset_custom_on_model_creating_action()
        {
            var contextInfo
                = new DbContextInfo(
                      typeof(ContextWithExternalOnModelCreating3))
                {
                OnModelCreating = mb => mb.Ignore <FakeEntity>()
                };

            contextInfo.OnModelCreating = null;

            var objectContext = ((IObjectContextAdapter)contextInfo.CreateInstance()).ObjectContext;

            Assert.NotNull(objectContext);
            Assert.True(objectContext.CreateDatabaseScript().Contains("FakeEntities"));
        }
        public void CreateInstance_should_use_passed_provider_info_when_building_model_even_when_config_is_passed_as_well()
        {
            var config = AddConnectionStrings(
                CreateEmptyConfig().AddDefaultConnectionFactory(
                    typeof(FakeDbContextInfoConnectionFactory).FullName + ", EntityFramework.UnitTests",
                    new string[0]));

            var contextInfo = new DbContextInfo(typeof(SimpleContext), config, ProviderRegistry.SqlCe4_ProviderInfo);

            Assert.Equal(ProviderRegistry.SqlCe4_ProviderInfo.ProviderInvariantName, contextInfo.ConnectionProviderName);
            Assert.Equal(string.Empty, contextInfo.ConnectionString);

            Database.SetInitializer <SimpleContext>(null);

            var objectContext = ((IObjectContextAdapter)contextInfo.CreateInstance()).ObjectContext;

            Assert.NotNull(objectContext);
            Assert.Equal("SqlCeConnection", ((EntityConnection)objectContext.Connection).StoreConnection.GetType().Name);
        }
        public void Can_set_hard_coded_connection_from_supplied_config()
        {
            var connection  = new DbConnectionInfo("GetMeFromSuppliedConfig");
            var contextInfo = new DbContextInfo(
                typeof(SimpleContext),
                CreateEmptyConfig().AddConnectionString(
                    "GetMeFromSuppliedConfig", "Database=ConnectionFromSuppliedConfig", "System.Data.SqlClient"),
                connection);

            Assert.Equal(DbConnectionStringOrigin.DbContextInfo, contextInfo.ConnectionStringOrigin);
            Assert.Equal("System.Data.SqlClient", contextInfo.ConnectionProviderName);
            Assert.Equal("GetMeFromSuppliedConfig", contextInfo.ConnectionStringName);
            Assert.True(contextInfo.IsConstructible);

            using (var context = contextInfo.CreateInstance())
            {
                Assert.Equal("ConnectionFromSuppliedConfig", context.Database.Connection.Database);
            }
        }
示例#15
0
        [Fact] // CodePlex 291, 1316
        public void CreateInstance_should_not_cause_database_initializer_to_run_even_if_context_consturctor_would_cause_it()
        {
            var contextInfo = new DbContextInfo(typeof(InitTestContext));

            using (var context = contextInfo.CreateInstance())
            {
                // Do something that would normally cause initialization
                Assert.NotNull(((IObjectContextAdapter)context).ObjectContext);
            }

            Assert.False(InitTestInitializer.HasRun);

            // Use context normally--initializer should now run.
            using (var context = new InitTestContext())
            {
                Assert.NotNull(((IObjectContextAdapter)context).ObjectContext);
            }

            Assert.True(InitTestInitializer.HasRun);
        }
        public void EdmxWriter_can_write_an_EDMX_when_DbContextInfo_is_used_to_specify_the_provider_info()
        {
            var contextInfo = new DbContextInfo(
                typeof(SimpleModelContext),
                new DbProviderInfo("System.Data.SqlClient", "2008"));

            using (var context = contextInfo.CreateInstance())
            {
                var edmxBuilder = new StringBuilder();

                EdmxWriter.WriteEdmx(context, XmlWriter.Create(edmxBuilder));

                SanityCheckEdmx(edmxBuilder);
            }
        }