示例#1
0
        private DbModel BuildDbModel(string schemaSpace)
        {
            DbModelBuilder modelBuilder = new DbModelBuilder();

            modelBuilder.Conventions.Remove <PluralizingTableNameConvention>();
            modelBuilder.Conventions.Add(conventions);

            foreach (CustomStoreConvention convention in conventions.OfType <CustomStoreConvention>())
            {
                convention.CurrentSchemaSpace = schemaSpace;
                convention.Reset();
            }

            IReadOnlyCollection <Type> entityTypes = schemaSpacesToEntities[schemaSpace];

            foreach (Type entityType in entityTypes)
            {
                modelBuilder.RegisterEntityType(entityType);
            }

            new PrivateCollectionMapping().MapPrivateCollections(modelBuilder); // TODO convert this to an EF convention

            IEnumerable <IModelDefinition> modelDefinitions = modelDefinitionDiscovery.DiscoverModelDefinitions();

            foreach (IModelDefinition modelDefinition in modelDefinitions)
            {
                modelDefinition.OnModelCreating(modelBuilder);
            }

            DbConnection dbConnection = Database.DefaultConnectionFactory.CreateConnection(connectionConfiguration.NameOrConnectionString);

            return(modelBuilder.Build(dbConnection));
        }
示例#2
0
        public DbContext CreateContext(string schemaSpace)
        {
            var modelDefinitions = modelDefinitionDiscovery.DiscoverModelDefinitions();

            var optionsBuilder = new DbContextOptionsBuilder <EntityDbContext>();

            configurers.ForEach(x => x.OnConfiguring(optionsBuilder));

            return(new EntityDbContext(
                       optionsBuilder.Options,
                       modelDefinitions.ToArray(),
                       conventions));
        }