public override void Customize(ModelBuilder modelBuilder, DbContext context)
        {
            base.Customize(modelBuilder, context);

            var contextType = context.GetType();
            var options     = context.GetDbOptions();
            var dbProvider  = context.GetDbProvider();

            //当 Shell 没创建时候不会产生任何的 Mapping
            var mappings = context.GetApplicationServiceProvider().GetServices <IEntityMappings>();

            foreach (var mapping in mappings)
            {
                DbContextSelectionAttribute att  = mapping.GetType().GetAttribute <DbContextSelectionAttribute>();
                DbContextAttribute          att2 = mapping.GetType().GetAttribute <DbContextAttribute>();
                if ((att == null && att2 == null && contextType.Equals(options.DefaultDbContext)) || (att?.ContextType == contextType || att2?.ContextType == contextType))
                {
                    mapping.ApplyMapping(modelBuilder, dbProvider);
                }
            }


            bool mapBuiltinEntities = options.IncludeBuiltinEntities(contextType);

            if (mapBuiltinEntities)
            {
                foreach (var mapping in this.GetBuiltinMappings())
                {
                    mapping.ApplyMapping(modelBuilder, dbProvider);
                }
            }
        }
Esempio n. 2
0
        private bool CanApply(TypeInfo migrationType, Type contextType)
        {
            DbContextAttribute          att1 = migrationType.GetCustomAttribute <DbContextAttribute>();
            DbContextSelectionAttribute att2 = migrationType.GetCustomAttribute <DbContextSelectionAttribute>();

            return((att1 == null && att2 == null) || (att1?.ContextType == contextType || att2?.ContextType == contextType));
        }
Esempio n. 3
0
        public void Create_attribute()
        {
            var attribute = new DbContextAttribute(typeof(MyContext));

            Assert.Same(typeof(MyContext), attribute.ContextType);
        }