public void Sets_are_initialized_but_do_not_change_model_using_name_and_model_constructor_on_DbContext()
 {
     var model = new DbModelBuilder().Build(ProviderRegistry.Sql2008_ProviderInfo).Compile();
     using (var context = new SimpleModelContextWithNoData(DefaultDbName<EmptyContext>(), model))
     {
         Assert.NotNull(context.Products);
         Assert.NotNull(context.Categories);
         context.Assert<Product>().IsNotInModel();
         context.Assert<Category>().IsNotInModel();
     }
 }
 public void SaveChangesAsync_is_virtual()
 {
     using (DbContext context = new SimpleModelContextWithNoData())
     {
         Assert.Equal(0, context.SaveChangesAsync().Result);
         Assert.True(((SimpleModelContextWithNoData)context).SaveChangesCalled);
     }
 }
 public void DbContext_construction_does_not_throw_but_subsequent_calls_using_connection_throw_for_nonexistent_connection_string()
 {
     var context = new SimpleModelContextWithNoData("Name=NonexistentConnectionString");
     Assert.Throws<InvalidOperationException>(() => GetObjectContext(context)).ValidateMessage(
         "DbContext_ConnectionStringNotFound", "NonexistentConnectionString");
 }
 public void DbContext_construction_does_not_throw_but_subsequent_calls_using_connection_throw_for_invalid_sql_connection_string()
 {
     var context = new SimpleModelContextWithNoData("InvalidKeywordConnectionString");
     Assert.Throws<ArgumentException>(() => GetObjectContext(context)).ValidateMessage(
         typeof(DbConnection).Assembly, "ADP_KeywordNotSupported", null);
 }
 public void DbContext_construction_does_not_throw_but_subsequent_calls_using_connection_throw_for_invalid_provider_keyword()
 {
     var context = new SimpleModelContextWithNoData("InvalidProviderNameConnectionString");
     Assert.Throws<ArgumentException>(() => GetObjectContext(context)).ValidateMessage(
         typeof(DbConnection).Assembly, "ConfigProviderNotFound", null);
 }
        private void Verify_DbContext_construction_using_connection_string_ctor(string nameOrConnectionString,
                                                                                string expectedDatabaseName =
                                                                                    "SimpleModel.SimpleModelContextWithNoData")
        {
            using (var context = new SimpleModelContextWithNoData(nameOrConnectionString))
            {
                Assert.NotNull(context.Products);
                Assert.NotNull(context.Categories);
                context.Assert<Product>().IsInModel();
                context.Assert<Category>().IsInModel();

                Assert.Equal(expectedDatabaseName, context.Database.Connection.Database);
            }
        }
        private void DbContext_construction_using_connection_string_and_model_Ctor(
            ConnectionStringFormat connStringFormat, DbCompiledModelContents modelContents)
        {
            // Act
            // Setup connection string
            string connectionString = null;
            string dbName = null;
            switch (connStringFormat)
            {
                case ConnectionStringFormat.DatabaseName:
                    connectionString = dbName = DefaultDbName<SimpleModelContextWithNoData>();
                    break;
                case ConnectionStringFormat.NamedConnectionString:
                    connectionString = "SimpleModelWithNoDataFromAppConfig";
                    dbName = "SimpleModel.SimpleModelWithNoData";
                    break;
                case ConnectionStringFormat.ProviderConnectionString:
                    connectionString = SimpleConnectionString<SimpleModelContextWithNoData>();
                    dbName = "SimpleModel.SimpleModelContextWithNoData";
                    break;
                default:
                    throw new ArgumentException("Invalid ConnectionStringFormat enum supplied " + connStringFormat);
            }

            // Setup DbCompiledModel
            var builder = new DbModelBuilder();

            switch (modelContents)
            {
                case DbCompiledModelContents.IsEmpty:
                    // Do nothing as builder has already been initialized
                    break;
                case DbCompiledModelContents.IsSubset:
                    // Product is not defined here
                    builder.Entity<Category>();
                    break;
                case DbCompiledModelContents.IsSuperset:
                    builder.Entity<Category>();
                    builder.Entity<Product>();
                    builder.Entity<Login>();
                    break;
                case DbCompiledModelContents.Match:
                    builder.Entity<Category>();
                    builder.Entity<Product>();
                    break;
                case DbCompiledModelContents.DontMatch:
                    builder.Entity<FeaturedProduct>();
                    builder.Entity<Login>();
                    break;
                default:
                    throw new ArgumentException("Invalid DbCompiledModelContents Arguments passed in, " + modelContents);
            }

            // Act
            using (
                var context = new SimpleModelContextWithNoData(connectionString,
                                                               builder.Build(ProviderRegistry.Sql2008_ProviderInfo).
                                                                   Compile()))
            {
                // Assert
                Assert.Equal(context.Database.Connection.Database, dbName);
                switch (modelContents)
                {
                    case DbCompiledModelContents.IsEmpty:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Categories);
                        context.Assert<Category>().IsNotInModel();
                        context.Assert<Product>().IsNotInModel();
                        break;
                    case DbCompiledModelContents.IsSubset:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Categories);
                        context.Assert<Category>().IsInModel();
                        context.Assert<Product>().IsInModel(); // reachability
                        break;
                    case DbCompiledModelContents.IsSuperset:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Categories);
                        context.Assert<Category>().IsInModel();
                        context.Assert<Product>().IsInModel();
                        context.Assert<Login>().IsInModel();
                        break;
                    case DbCompiledModelContents.Match:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Categories);
                        context.Assert<Category>().IsInModel();
                        context.Assert<Product>().IsInModel();
                        context.Assert<Login>().IsNotInModel();
                        break;
                    case DbCompiledModelContents.DontMatch:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Products);
                        context.Assert<Login>().IsInModel();
                        context.Assert<FeaturedProduct>().IsInModel();
                        break;
                    default:
                        throw new ArgumentException("Invalid DbCompiledModelContents Arguments passed in, " +
                                                    modelContents);
                }
            }
        }
        private void Verify_DbContext_construction_using_connection_and_model_Ctor(
            DbConnection connection,
            DbCompiledModelContents contents)
        {
            // Arrange
            // DbCompiledModel creation as appropriate for the various model content options
            var builder = new DbModelBuilder();

            switch (contents)
            {
                case DbCompiledModelContents.IsEmpty:
                    // Do nothing as builder has already been initialized
                    break;
                case DbCompiledModelContents.IsSubset:
                    // Product is not defined here
                    builder.Entity<Category>();
                    break;
                case DbCompiledModelContents.IsSuperset:
                    builder.Entity<Category>();
                    builder.Entity<Product>();
                    builder.Entity<Login>();
                    break;
                case DbCompiledModelContents.Match:
                    builder.Entity<Category>();
                    builder.Entity<Product>();
                    break;
                case DbCompiledModelContents.DontMatch:
                    builder.Entity<FeaturedProduct>();
                    builder.Entity<Login>();
                    break;
                default:
                    throw new ArgumentException("Invalid DbCompiledModelContents Arguments passed in, " + contents);
            }

            var model = builder.Build(connection).Compile();

            // Act
            using (var context = new SimpleModelContextWithNoData(connection, model))
            {
                // Verification as appropriate for the various model content options
                switch (contents)
                {
                    case DbCompiledModelContents.IsEmpty:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Categories);
                        context.Assert<Category>().IsNotInModel();
                        context.Assert<Product>().IsNotInModel();
                        break;
                    case DbCompiledModelContents.IsSubset:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Categories);
                        context.Assert<Category>().IsInModel();
                        context.Assert<Product>().IsInModel(); // Reachability
                        break;
                    case DbCompiledModelContents.IsSuperset:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Categories);
                        context.Assert<Category>().IsInModel();
                        context.Assert<Product>().IsInModel();
                        context.Assert<Login>().IsInModel();
                        break;
                    case DbCompiledModelContents.Match:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Categories);
                        context.Assert<Category>().IsInModel();
                        context.Assert<Product>().IsInModel();
                        context.Assert<Login>().IsNotInModel();
                        break;
                    case DbCompiledModelContents.DontMatch:
                        Assert.NotNull(context.Categories);
                        Assert.NotNull(context.Products);
                        context.Assert<Login>().IsInModel();
                        context.Assert<FeaturedProduct>().IsInModel();
                        break;
                    default:
                        throw new ArgumentException("Invalid DbCompiledModelContents Arguments passed in, " + contents);
                }
            }
        }
 public void DbContext_construction_does_not_throw_but_subsequent_calls_using_connection_throw_for_invalid_SqlCE_connection_string()
 {
     var sqlCeAssembly =
         new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0").CreateConnection("Dummy").GetType().Assembly;
     var context = new SimpleModelContextWithNoData("Data Sourc=Scenario_Use_AppConfig.sdf");
     Assert.Throws<ArgumentException>(() => GetObjectContext(context)).ValidateMessage(
         sqlCeAssembly,
         "ADP_KeywordNotSupported",
         "System.Data.SqlServerCe",
         "data sourc");
 }
        public void Scenario_Use_AppConfig_connection_string()
        {
            Database.Delete("Scenario_Use_AppConfig_connection_string");

            using (var context = new SimpleModelContextWithNoData("Scenario_Use_AppConfig_connection_string"))
            {
                Assert.Equal("Scenario_Use_AppConfig", context.Database.Connection.Database);
                InsertIntoCleanContext(context);
            }

            using (var context = new SimpleModelContextWithNoData("Scenario_Use_AppConfig_connection_string"))
            {
                ValidateFromCleanContext(context);
            }
        }
 private void InsertIntoCleanContext(SimpleModelContextWithNoData context)
 {
     context.Categories.Add(new Category() { Id = "Large Hadron Collider" });
     context.Products.Add(new Product() { Name = "Higgs Boson", CategoryId = "Large Hadron Collider" });
     context.SaveChanges();
 }
        private void ValidateFromCleanContext(SimpleModelContextWithNoData context)
        {
            var product = context.Products.Find(1);
            var category = context.Categories.Find("Large Hadron Collider");

            // Scenario ends; simple validation of final state follows
            Assert.NotNull(product);
            Assert.Equal(EntityState.Unchanged, GetStateEntry(context, product).State);

            Assert.NotNull(category);
            Assert.Equal(EntityState.Unchanged, GetStateEntry(context, category).State);

            Assert.Equal("Large Hadron Collider", product.CategoryId);
            Assert.Same(category, product.Category);
            Assert.True(category.Products.Contains(product));
        }
        public void Scenario_CodeFirst_with_ModelBuilder()
        {
            Database.Delete("Scenario_CodeFirstWithModelBuilder");

            var builder = new DbModelBuilder();

            builder.Entity<Product>();
            builder.Entity<Category>();

            var model = builder.Build(ProviderRegistry.Sql2008_ProviderInfo).Compile();

            using (var context = new SimpleModelContextWithNoData("Scenario_CodeFirstWithModelBuilder", model))
            {
                InsertIntoCleanContext(context);
            }

            using (var context = new SimpleModelContextWithNoData("Scenario_CodeFirstWithModelBuilder", model))
            {
                ValidateFromCleanContext(context);
            }
        }
示例#14
0
 public void DbContext_construction_does_not_throw_but_subsequent_calls_using_connection_throw_for_invalid_provider_keyword()
 {
     var context = new SimpleModelContextWithNoData("InvalidProviderNameConnectionString");
     Assert.Throws<ArgumentException>(() => GetObjectContext(context)).ValidateMessage("EntityClient_InvalidStoreProvider");
 }