Exemplo n.º 1
0
        public void BindDbContext <TTableInterface, TDbContextType>(DatabaseBind bind)
        {
            bind.TableInterface = typeof(TTableInterface);
            bind.DbContextType  = typeof(TDbContextType);

            bind.Entities = GetAllEntityTypes(bind).ToList();

            if (bind.SlaveConnections == null)
            {
                bind.SlaveConnections = new List <DbConnection>();
            }

            if (bind.SlaveConnections.Count == 0)
            {
                bind.SlaveConnections.Add(bind.MasterConnection);
            }

            // random
            bind.SlaveId = new Random().Next(bind.SlaveConnections.Count);

            DbContextBinds.Add(bind);

            if (bind.CreateDbIfNotExist)
            {
                GetMaster(bind.TableInterface).Database.EnsureCreated();
            }
        }
Exemplo n.º 2
0
        public IMongoCollection <T> Collection <T>(string name = "") where T : class
        {
            Type entityType = typeof(T);

            DatabaseBind binding = DbContextBinds.First(x => x.GetType().Equals(typeof(DatabaseBind))) as DatabaseBind;
            var          db      = GetMaster(entityType);

            return((db as DbContext4MongoDb).Set <T>(name));
        }
Exemplo n.º 3
0
        /*public IMongoCollection<T> Collection<T>(string collection) where T : class
         * {
         *  DatabaseBind binding = GetBinding(typeof(T));
         *  if (binding.DbContextMaster == null)
         *  {
         *      binding.DbContext = new MongoDbContext(binding.ConnectionString);
         *  }
         *
         *  return binding.DbContextSlavers.First();
         * }*/

        public Object Find(Type type, params string[] keys)
        {
            DatabaseBind binding = DbContextBinds.First(x => x.Entities.Contains(type));

            if (binding.DbContextMaster == null || binding.DbContextMaster.Database.CurrentTransaction == null)
            {
                return(GetReader(type).Find(type, keys));
            }
            else
            {
                return(GetMaster(type).Find(type, keys));
            }
        }
        public void BindDbContext <TTableInterface, TDbContextType>(DatabaseBind bind)
        {
            bind.TableInterface = typeof(TTableInterface);
            bind.DbContextType  = typeof(TDbContextType);

            bind.Entities = GetAllEntityTypes(bind).ToList();

            DbContextBinds.Add(bind);

            if (bind.CreateDbIfNotExist)
            {
                GetMaster(bind.TableInterface).Database.EnsureCreated();
            }
        }
Exemplo n.º 5
0
        public DbSet <T> Table <T>() where T : class
        {
            Type entityType = typeof(T);

            DatabaseBind binding = DbContextBinds.First(x => x.Entities.Contains(entityType));

            if (binding.DbContextMaster == null || binding.DbContextMaster.Database.CurrentTransaction == null)
            {
                return(GetReader(entityType).Set <T>());
            }
            else
            {
                return(GetMaster(entityType).Set <T>());
            }
        }
Exemplo n.º 6
0
        public void BindDbContext(DatabaseBind bind)
        {
            bind.Entities = GetAllEntityTypes(bind).ToList();

            DbContextBinds.Add(bind);
        }
Exemplo n.º 7
0
        private List <Type> GetAllEntityTypes(DatabaseBind bind)
        {
            var assemblies = (string[])AppDomain.CurrentDomain.GetData("Assemblies");

            return(Utility.GetClassesWithInterface(bind.TableInterface, assemblies));
        }
Exemplo n.º 8
0
        public int SaveChanges()
        {
            DatabaseBind binding = DbContextBinds.Where(x => x.DbContextType != null).First(x => x.DbContextMaster != null && x.DbContextMaster.Database.CurrentTransaction != null);

            return(binding.DbContextMaster.SaveChanges());
        }