예제 #1
0
 public async Task <IEnumerable <Game> > GetWaitingGames()
 {
     using (IDbContextReadOnlyScope contextScope = _dbContextFactory.CreateReadOnly())
     {
         return(await _gameService.GetWaitingGames());
     }
 }
예제 #2
0
        public User GetUser(Guid userId)
        {
            /*
             * An example of using DbContextScope for read-only queries.
             * Here, we access the Entity Framework DbContext directly from
             * the business logic service class.
             *
             * Calling SaveChanges() is not necessary here (and in fact not
             * possible) since we created a read-only scope.
             */
            using (IDbContextReadOnlyScope dbContextScope = _dbContextScopeFactory.CreateReadOnly())
            {
                UserManagementDbContext dbContext = dbContextScope.DbContexts.Get <UserManagementDbContext>();
#if EF6
                User user = dbContext.Users.Find(userId);
#elif EFCore
                var user = dbContext.Users.SingleOrDefault(x => x.Id == userId);
#endif

                if (user == null)
                {
                    throw new ArgumentException($"Invalid value provided for userId: [{userId}]. Couldn't find a user with this ID.");
                }

                return(user);
            }
        }
예제 #3
0
 public static IQueryable <T> SqlQuery <T>(
     this IDbContextReadOnlyScope ctx,
     string query,
     params object[] Parametros
     )
 {
     return(ctx.DbContexts.Get <ApplicationDbContext>().Database.SqlQuery <T>(query, Parametros).AsQueryable());
 }
예제 #4
0
 public static int ExecuteCommand(
     this IDbContextReadOnlyScope ctx,
     string query,
     params object[] Parametros
     )
 {
     return(ctx.DbContexts.Get <ApplicationDbContext>().Database.ExecuteSqlCommand(query, Parametros));
 }
예제 #5
0
 public IEnumerable <User> GetUsers(params Guid[] userIds)
 {
     using (IDbContextReadOnlyScope dbContextScope = _dbContextScopeFactory.CreateReadOnly())
     {
         UserManagementDbContext dbContext = dbContextScope.DbContexts.Get <UserManagementDbContext>();
         return(dbContext.Users.Where(u => userIds.Contains(u.Id)).ToList());
     }
 }
예제 #6
0
 protected T RunInContextScope <T>(Func <T> function, bool isReadOnly = false)
 {
     if (isReadOnly)
     {
         using (IDbContextReadOnlyScope dbContextScope = _dbContextScopeFactory.CreateReadOnly())
         {
             return(function());
         }
     }
     else
     {
         using (IDbContextScope dbContextScope = _dbContextScopeFactory.Create())
         {
             T result = function();
             dbContextScope.SaveChanges();
             return(result);
         }
     }
 }
예제 #7
0
 /// <summary>
 /// 获取只读DB
 /// </summary>
 /// <param name="scope">上下文</param>
 /// <returns></returns>
 public DbContext GetDb(IDbContextReadOnlyScope scope)
 {
     return(scope.DbContexts.Get <JuCheapContext>());
 }
예제 #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ReadOnlyServiceTransaction"/> class.
        /// </summary>
        /// <param name="dbContextScopeFactory">The <see cref="IDbContextScopeFactory"/> used to
        /// create instances of <see cref="IDbContextScope"/> as they are needed by context
        /// operations.</param>
        /// <exception cref="ArgumentNullException"><paramref name="dbContextScopeFactory"/> is
        /// <see langword="null"/>.</exception>
        internal ReadOnlyServiceTransaction(IDbContextScopeFactory dbContextScopeFactory)
        {
            ParameterValidation.IsNotNull(dbContextScopeFactory, nameof(dbContextScopeFactory));

            this.dbContextReadOnlyScope = dbContextScopeFactory.CreateReadOnly();
        }
예제 #9
0
 protected DbContext GetDb(IDbContextReadOnlyScope scope)
 {
     return(scope.DbContexts.Get <ToolContext>());
 }
예제 #10
0
        public IQueryable <string> TestMethod3_sub(IDbContextReadOnlyScope scope)
        {
            var db = scope.DbContexts.Get <CommonDbContext>();;

            return(db.Users.Select(x => x.Name));
        }
예제 #11
0
 public static DbSet <T> GetEntity <T>(this IDbContextReadOnlyScope value) where T : class
 {
     return(value.DbContexts.Get <ApplicationDbContext>().Set <T>());
 }
예제 #12
0
 public ReadOnlyUnitOfWork(IDbContextScopeFactory dbContextScopeFactory, IRepositoryLocator repositoryLocator)
 {
     this.dbContextScope    = dbContextScopeFactory.CreateReadOnly();
     this.repositoryLocator = repositoryLocator;
 }