public async Task <TEntity> AddAsync(TEntity entity) { m_context.Set <TEntity>().Add(entity); await m_context.SaveChangesAsync(); return(entity); }
public async Task AddAsync(Coupon coupon) { await _coupons.AddAsync(coupon); await _context.SaveChangesAsync(); }
public async Task BulkAddAsync(List <TEntity> entities) { await DbContext.AddRangeAsync(entities); await DbContext.SaveChangesAsync(); }
public async Task It_throws_object_disposed_exception(bool async) { var context = new DbContext(new DbContextOptions <DbContext>()); if (async) { await context.DisposeAsync(); } else { context.Dispose(); } // methods (tests all paths) Assert.StartsWith( CoreStrings.ContextDisposed, Assert.Throws <ObjectDisposedException>(() => context.Add(new object())).Message); Assert.StartsWith( CoreStrings.ContextDisposed, Assert.Throws <ObjectDisposedException>(() => context.Find(typeof(Random), 77)).Message); Assert.StartsWith( CoreStrings.ContextDisposed, Assert.Throws <ObjectDisposedException>(() => context.Attach(new object())).Message); Assert.StartsWith( CoreStrings.ContextDisposed, Assert.Throws <ObjectDisposedException>(() => context.Update(new object())).Message); Assert.StartsWith( CoreStrings.ContextDisposed, Assert.Throws <ObjectDisposedException>(() => context.Remove(new object())).Message); Assert.StartsWith( CoreStrings.ContextDisposed, Assert.Throws <ObjectDisposedException>(() => context.SaveChanges()).Message); Assert.StartsWith( CoreStrings.ContextDisposed, (await Assert.ThrowsAsync <ObjectDisposedException>(() => context.SaveChangesAsync())).Message); Assert.StartsWith( CoreStrings.ContextDisposed, (await Assert.ThrowsAsync <ObjectDisposedException>(() => context.AddAsync(new object()).AsTask())).Message); Assert.StartsWith( CoreStrings.ContextDisposed, (await Assert.ThrowsAsync <ObjectDisposedException>(() => context.FindAsync(typeof(Random), 77).AsTask())).Message); var methodCount = typeof(DbContext).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Count(); var expectedMethodCount = 42 + 8; Assert.True( methodCount == expectedMethodCount, userMessage: $"Expected {expectedMethodCount} methods on DbContext but found {methodCount}. " + "Update test to ensure all methods throw ObjectDisposedException after dispose."); // getters Assert.StartsWith( CoreStrings.ContextDisposed, Assert.Throws <ObjectDisposedException>(() => context.ChangeTracker).Message); Assert.StartsWith( CoreStrings.ContextDisposed, Assert.Throws <ObjectDisposedException>(() => context.Model).Message); var expectedProperties = new List <string> { nameof(DbContext.ChangeTracker), nameof(DbContext.ContextId), // By-design, does not throw for disposed context nameof(DbContext.Database), nameof(DbContext.Model) }; Assert.True( expectedProperties.SequenceEqual( typeof(DbContext) .GetProperties() .Select(p => p.Name) .OrderBy(s => s) .ToList()), userMessage: "Unexpected properties on DbContext. " + "Update test to ensure all getters throw ObjectDisposedException after dispose."); Assert.StartsWith( CoreStrings.ContextDisposed, Assert.Throws <ObjectDisposedException>(() => ((IInfrastructure <IServiceProvider>)context).Instance).Message); }
public async Task Can_add_update_delete_end_to_end_using_partial_shadow_state() { var model = new Model(); var customerType = model.AddEntityType(typeof(Customer)); var property1 = customerType.AddProperty("Id", typeof(int)); customerType.GetOrSetPrimaryKey(property1); customerType.AddProperty("Name", typeof(string)); var optionsBuilder = new DbContextOptionsBuilder() .UseModel(model) .UseInMemoryDatabase(nameof(ShadowStateUpdateTest)) .UseInternalServiceProvider(_fixture.ServiceProvider); var customer = new Customer { Id = 42 }; using (var context = new DbContext(optionsBuilder.Options)) { context.Add(customer); context.Entry(customer).Property("Name").CurrentValue = "Daenerys"; await context.SaveChangesAsync(); context.Entry(customer).Property("Name").CurrentValue = "Changed!"; } using (var context = new DbContext(optionsBuilder.Options)) { var customerFromStore = context.Set <Customer>().Single(); Assert.Equal(42, customerFromStore.Id); Assert.Equal( "Daenerys", (string)context.Entry(customerFromStore).Property("Name").CurrentValue); } using (var context = new DbContext(optionsBuilder.Options)) { var customerEntry = context.Entry(customer).GetInfrastructure(); customerEntry[customerType.FindProperty("Name")] = "Daenerys Targaryen"; context.Update(customer); await context.SaveChangesAsync(); } using (var context = new DbContext(optionsBuilder.Options)) { var customerFromStore = context.Set <Customer>().Single(); Assert.Equal(42, customerFromStore.Id); Assert.Equal( "Daenerys Targaryen", (string)context.Entry(customerFromStore).Property("Name").CurrentValue); } using (var context = new DbContext(optionsBuilder.Options)) { context.Remove(customer); await context.SaveChangesAsync(); } using (var context = new DbContext(optionsBuilder.Options)) { Assert.Equal(0, context.Set <Customer>().Count()); } }
public async Task <int> SaveChangesAsync() { return(await _dbContext.SaveChangesAsync()); }
public Task <int> SaveChangesAsync() { return(_context.SaveChangesAsync()); }