public async Task <IEnumerable <Game> > GetWaitingGames() { using (IDbContextReadOnlyScope contextScope = _dbContextFactory.CreateReadOnly()) { return(await _gameService.GetWaitingGames()); } }
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); } }
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()); }
public static int ExecuteCommand( this IDbContextReadOnlyScope ctx, string query, params object[] Parametros ) { return(ctx.DbContexts.Get <ApplicationDbContext>().Database.ExecuteSqlCommand(query, Parametros)); }
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()); } }
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); } } }
/// <summary> /// 获取只读DB /// </summary> /// <param name="scope">上下文</param> /// <returns></returns> public DbContext GetDb(IDbContextReadOnlyScope scope) { return(scope.DbContexts.Get <JuCheapContext>()); }
/// <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(); }
protected DbContext GetDb(IDbContextReadOnlyScope scope) { return(scope.DbContexts.Get <ToolContext>()); }
public IQueryable <string> TestMethod3_sub(IDbContextReadOnlyScope scope) { var db = scope.DbContexts.Get <CommonDbContext>();; return(db.Users.Select(x => x.Name)); }
public static DbSet <T> GetEntity <T>(this IDbContextReadOnlyScope value) where T : class { return(value.DbContexts.Get <ApplicationDbContext>().Set <T>()); }
public ReadOnlyUnitOfWork(IDbContextScopeFactory dbContextScopeFactory, IRepositoryLocator repositoryLocator) { this.dbContextScope = dbContextScopeFactory.CreateReadOnly(); this.repositoryLocator = repositoryLocator; }