public IQueryable <TEntity> GetAll() { try { return(_context.Set <TEntity>()); } catch (Exception ex) { throw new Exception($"Couldn't retrieve entities: {ex.Message}"); } }
static void Main(string[] args) { IHost host = CreateHostBuilder(args).Build(); Task task = host.RunAsync(); User user = new User() { Name = "111", Blogs = new System.Collections.Generic.List <Blog> { new Blog { Url = "htts://dev.haocai.com.cn", Posts = new System.Collections.Generic.List <Post> { new Post { Title = "1111" }, new Post { Title = "2222" } } } } }; EFDemoContext context = host.Services.GetService <EFDemoContext>(); context.Database.EnsureDeleted(); context.Database.Migrate(); EntityEntry <User> ent = context.Set <User>().Add(user); context.SaveChanges(); user.Name = "CCCC"; context.SaveChanges(); Console.ReadKey(); }
public async Task <bool> DeleteAsync(object obj) { var entity = await _context.Set <TEntity>().FindAsync(obj); if (entity == null) { throw new ArgumentNullException($"{nameof(DeleteAsync)} entity must not be null"); } try { _context.Set <TEntity>().Remove(entity); DisplayStates(_context.ChangeTracker.Entries()); await _context.SaveChangesAsync(); return(true); } catch (Exception ex) { throw new Exception($"{nameof(entity)} could not be deleted: {ex.Message}"); } }
public IQueryable <T> Get() { return(_context.Set <T>()); }