using DaSaSo.Domain.Model; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DaSaSo.EntityFramework.Services.Common { class NonQueryDataService where T: DomainObject { private readonly DaSaSoDbContextFactory _contextFactory; public NonQueryDataService(DaSaSoDbContextFactory contextFactory) { _contextFactory = contextFactory; } public async Task Create(T entity) { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { EntityEntry createdEntity = await context.Set().AddAsync(entity); await context.SaveChangesAsync(); return createdEntity.Entity; } } public T CreateNonAsync(T entity) { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { EntityEntry createdEntity = context.Set().Add(entity); context.SaveChanges(); return createdEntity.Entity; } } public async Task Delete(int id) { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { T entity = await context.Set().FirstOrDefaultAsync((e) => e.Id == id); context.Set().Remove(entity); await context.SaveChangesAsync(); return true; } } public async Task Update(int id, T entity) { using (DaSaSoDbContext context = _contextFactory.CreateDbContext()) { entity.Id = id; context.Set().Update(entity); await context.SaveChangesAsync(); return entity; } } } }