-
Notifications
You must be signed in to change notification settings - Fork 7
How to use Code
Mubassir Hasan edited this page Oct 26, 2018
·
2 revisions
Suppose we have a model named in Category
public int Id { get; set; }
public string Name{ get; set; }
public DateTime AddedDate{ get; set; }
public string Description { get; set; }
public int? CategoryId { get; set; }
//[ForeignKey("CategoryId")]
public Category Categoris { get; set; }
public ICollection<Category> CategoriesList { get; set; }
public ICollection<Product> Products { get; set; }
Now
Initialize IUnitOfWork
in constructor
How to create Constructor
Adding a new entry in controller Sync Method
_unitOfWork.Repository<Category>().Insert(new Category{
Name="Garments",
Description="Root category of clothings"
AddedDate=DateTime.Now;
});
Async Insert
await _unitOfWork.Repository<Category>().Insert(new Category{
Name="Garments",
Description="Root category of clothings"
AddedDate=DateTime.Now;
});
Update same as Insert first you need to fatch id
awit _unitOfWork.Repository<Category>().GetById(id);
Finding Releated data loading using Egder Loading.
_unitOfWork.Repository<Category>().GetAllInclude(x => x.Categoris,p=>p.Products);
Finding Releated data with where clause loading using Egder Loading. Getting mobile Finding all product under Mobile Category
_unitOfWork.Repository<Product>().GetIncludeList(c=>c.Category.Name=="Mobile",x => x.Categoris,p=>p.Products);
For Complex query
IQueryable<Product> dbProducts = _unitOfWork.Repository<Product>().Query()
.Include(x => x.Brand)
.Include(c => c.Category)
.Include(u => u.Unit)
.Include(pc => pc.ProductCommentses)
.Include(pi => pi.ProductImages)
.Include(ps => ps.ProductStocks);
if (ctgid>0)
{
dbProducts = dbProducts.Where(x => x.CategoryId == ctgid ||x.Category.Id==ctgid);
}
dbProducts = dbProducts.OrderByDescending(x => x.AddedDate).Take(take);