AlinSpace.Database.EfCore
Database abstraction layer for EntityFramework Core.
Why?
You can use naked EntityFramework Core, but I think using this abstraction layer will make it simpler to use.
Features
- Repository: Automatically creates Repositories.
- Auto Include: Automatically includes marked children with tags.
- Cascade Soft Delete: Automacially soft deletes entity and children marked with tags.
- And many small helper functionalities.
Example
Create a simple model:
public class Book : AbstractEntityWithId<Guid>
{
public string Title { get; set; }
}
Then create a database context:
public class MyDatabaseContext : AbstractDbContext
{
public DbSet<Book> Book { get; set; }
public MyDatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
}
Then create the options and setup the transaction like this:
// Prepare database context options.
var options = optionsBuilder.Build();
// Create the transaction object.
var transaction = TransactionFactory.Create<MyDatabaseContext>(options);
// Get repository. The primary key can be strongly-typed.
var bookRepository = transaction.GetRepository<Book, Guid>();
// Insert new entity.
var book = new Book();
await bookRepository.InsertAsync(book, commit: true);
// Get entity.
var bookReceived = await bookRepository.GetAsync(book.Id);
// Update entity.
bookReceived.Title = "Rich Dad Poor Dad";
await bookRepository.UpdateAsync(bookReceived, commit: true);
// Delete entity.
await bookRepository.DeleteAsync(bookReceived.Id, commit: true);
// Find entity.
var foundBook = await bookRepository.FindFirstAsync(view => view.Filter = q => q.Where(x => x.Title.Contains("Rich Dad")));
// Create a custom query.
var query = bookRepository.NewQuery();
// Commit changes.
await transaction.CommitAsync();
Read more
()[]