services.AddUnitOfWork<BloggingContext>(setup =>
setup.UseSqlServer(
"Data Source=(localdb)\\MSSqlLocalDb;Initial Catalog=UnitOfWorkDb;Integrated Security=true;MultipleActiveResultSets=true;"
), typeof(Startup).GetTypeInfo().Assembly);
private readonly IRepository<Blog> _repository;
private readonly IUnitOfWork _unitOfWork;
public BlogsController(IRepository<Blog> repository, IUnitOfWork unitOfWork)
{
_repository = repository;
_unitOfWork = unitOfWork;
}
public class BloggingContext : DbContextBase
The entity extends EntityTypeConfiguration, it will auto apply into dbcontext as table
public class Blog : TrackedAndSoftDelete
{
public string Url { get; set; }
}
public class BlogMap : EntityTypeConfiguration<Blog>
{
public override void Configure(EntityTypeBuilder<Blog> builder)
{
builder.ToTable("Blogs");
builder.Property(x => x.Url).HasMaxLength(100);
base.Configure(builder);
}
}
The entity extends QueryTypeConfiguration, it will auto apply into dbcontext as table
public class BlogsView
{
public string Url { get; set; }
}
public class BlogsViewMap : QueryTypeConfiguration<BlogsView>
{
public override void Configure(QueryTypeBuilder<BlogsView> builder)
{
builder.ToView("BolgViews");
base.Configure(builder);
}
}
var user = _repository.Insert(new Blog() { Url = value });
_unitOfWork.Commit();
var entity = _repository.Table.FirstOrDefault(x => x.Id == id);
entity.Url = value;
_repository.Update(entity);
_unitOfWork.Commit();
var entity = _repository.Table.FirstOrDefault(x => x.Id == id);
_repository.Delete(entity);
using (var tran = _unitOfWork.BeginTransaction())
{
......
_unitOfWork.Commit();
tran.Commit();
}
var list = _repository.Table.ToList().Select(x => x.Url).ToArray();
_unitOfWork.Commit();
var paging= _repository.Table.ToPaginatedList(1,20);