Exemple #1
0
        static void Main(string[] args)
        {
            Database.SetInitializer <ListContext>(null);

            TenantInitializer.EnsureTenantInitialized("john");
            using (var db = new ListContext("john"))
            {
                db.Lists.Add(new List
                {
                    Name        = "My List",
                    Description = "My latest list of stuff",
                    Columns     = new List <ListColumn>
                    {
                        new ListColumn {
                            Heading = "Thing"
                        },
                        new ListColumn {
                            Heading = "About the thing"
                        }
                    }
                });
                db.SaveChanges();
            }

            TenantInitializer.EnsureTenantInitialized("jane");
            using (var db = new ListContext("jane"))
            {
                db.Lists.Add(new List
                {
                    Name        = "My List",
                    Description = "Jad's latest list of stuff",
                    Columns     = new List <ListColumn>
                    {
                        new ListColumn {
                            Heading = "Name"
                        },
                        new ListColumn {
                            Heading = "Description"
                        }
                    }
                });
                db.SaveChanges();
            }
        }
Exemple #2
0
        public static void EnsureTenantInitialized(string schema)
        {
            using (var db = new ListContext(schema))
            {
                // Ensure that the physical database exists (create an empty one if not)
                using (var b = new BlankContext(db.Database.Connection))
                {
                    if (!b.Database.Exists())
                    {
                        ((IObjectContextAdapter)b).ObjectContext.CreateDatabase();
                    }
                }

                // Ensure the schema exists (migrations won't create it since we are messing with object names)
                db.Database.ExecuteSqlCommand(string.Format(_schemaCreationSql, schema));

                // Apply migrations redirected to the schema
                var config = new Configuration();
                config.SetSqlGenerator(SqlProviderServices.ProviderInvariantName, new SchemaRenamingMigrationSqlGenerator(schema));
                config.SetHistoryContextFactory(SqlProviderServices.ProviderInvariantName, (c, s) => new HistoryContext(c, schema));
                new DbMigrator(config).Update();
            }
        }
        public static void EnsureTenantInitialized(string schema)
        {
            using (var db = new ListContext(schema))
            {
                // Ensure that the physical database exists (create an empty one if not)
                using (var b = new BlankContext(db.Database.Connection))
                {
                    if (!b.Database.Exists())
                    {
                        ((IObjectContextAdapter)b).ObjectContext.CreateDatabase();
                    }
                }

                // Ensure the schema exists (migrations won't create it since we are messing with object names)
                db.Database.ExecuteSqlCommand(string.Format(_schemaCreationSql, schema));

                // Apply migrations redirected to the schema
                var config = new Configuration();
                config.SetSqlGenerator(SqlProviderServices.ProviderInvariantName, new SchemaRenamingMigrationSqlGenerator(schema));
                config.SetHistoryContextFactory(SqlProviderServices.ProviderInvariantName, (c, s) => new HistoryContext(c, schema));
                new DbMigrator(config).Update();
            }
        }
        static void Main(string[] args)
        {
            Database.SetInitializer<ListContext>(null);

            TenantInitializer.EnsureTenantInitialized("john");
            using (var db = new ListContext("john"))
            {
                db.Lists.Add(new List
                {
                    Name = "My List",
                    Description = "My latest list of stuff",
                    Columns = new List<ListColumn>
                    {
                        new ListColumn { Heading = "Thing" },
                        new ListColumn { Heading = "About the thing" }
                    }
                });
                db.SaveChanges();
            }

            TenantInitializer.EnsureTenantInitialized("jane");
            using (var db = new ListContext("jane"))
            {
                db.Lists.Add(new List
                {
                    Name = "My List",
                    Description = "Jad's latest list of stuff",
                    Columns = new List<ListColumn>
                    {
                        new ListColumn { Heading = "Name" },
                        new ListColumn { Heading = "Description" }
                    }
                });
                db.SaveChanges();
            }
        }