Eager Loading of Navigation Properties - entity-framework

In my repository I'm trying to use eager loading to load related entities. Not sure why but it seems like when I return all instances of a particular entity, related entities are returned, but when I limit the results returned the related entities are not included in the results.
This code in the service layer is returning all orders, including related Customer, OrderItem, and Product entities:
public async Task<IEnumerable<Order>> GetOrdersAsync()
{
return await _repository.GetAsync(null, q => q.OrderByDescending(p => p.CreatedDate), "Customer", "OrderItems", "OrderItems.Product");
}
In the repository:
public async Task<IEnumerable<Order>> GetAsync(Expression<Func<Order, bool>> where = null, Func<IQueryable<Order>, IOrderedQueryable<Order>> orderBy = null, params string[] navigationProperties)
{
IQueryable<Order> query = _context.Set<Order>();
if (where != null)
{
query = query.Where(where);
}
//Apply eager loading
foreach (string navigationProperty in navigationProperties)
query = query.Include(navigationProperty);
if (orderBy != null)
{
return await orderBy(query).ToListAsync();
}
else
{
return await query.ToListAsync();
}
}
This code in the service layer is getting an order by id, but for whatever reason is not returning related Customer, OrderItem, and Product entities:
public async Task<Order> GetOrderByIdAsync(long id)
{
return await _repository.GetByIdAsync(id, "Customer", "OrderItems", "OrderItems.Product");
}
In the repository:
public async Task<Order> GetByIdAsync(long id, params string[] navigationProperties)
{
DbSet<Order> dbSet = _context.Set<Order>();
foreach (string navigationProperty in navigationProperties)
dbSet.Include(navigationProperty);
return await dbSet.FindAsync(id);
}
The one difference I see between the two repository methods is one is casting the _context.Set() to IQueryable before including navigation properties, while the other is calling Include directly on the DbSet itself. Should this matter?

So the repository method that was not working was using DBSet.Include incorrectly. Include is actually a method on the DBQuery class, which DBSet inherits from. I needed to use DBQuery.Include to replace the query with a new instance of the query for each navigation property I included. So I changed the implementation to:
public async Task<Order> GetByIdAsync(long id, params string[] navigationProperties)
{
DbQuery<Order> dbQuery = _context.Set<Order>();
foreach (string navigationProperty in navigationProperties)
dbQuery = dbQuery.Include(navigationProperty);
return await dbQuery.Where(o => o.Id == id).FirstOrDefaultAsync();
}

Related

Delete multiple row with ids without foreach and postman test

I have a little problem. But I dont know why it doesnt work. And I dont know how to post all ids by postman.
I am using unit of work with generic repository. I want to send int[] ids to my controller. I dont want to send entity. I searched a lot it today. And I changed my code. But what is problem now?
This is my repostiroy:
public async Task DeleteRangeAsync(Expression<Func<T, bool>> predicate)
{
IQueryable<T> query = _dbSet.Where(predicate);
await Task.Run(() => { _dbSet.RemoveRange(query.AsNoTracking()); });
}
This is my KulturManager:
public async Task<IResult> HardDeleteRangeAsync(int[] ids)
{
await UnitOfWork.Kulturs.DeleteRangeAsync(c => ids.Contains(c.Id));
await UnitOfWork.SaveAsync();
return new Result(ResultStatus.Success, Messages.Info("Kultur", "HardDelete"));
}
And this is my KulturController:
[HttpDelete("{ids}")]
public async Task<IActionResult> HardDeleteRangeAsync(int[] ids)
{
var result = await _kulturManager.HardDeleteRangeAsync(ids);
return Ok(result.Message);
}
Thank you for help
You shouldn't fetch all the entities you want to delete. Instead create stub entities for RemoveRange. If you don't have a common base class, this requires reflection, but with a common entity base class you can do it like this:
public void DeleteRange<T>(int[] ids) where T: BaseEntity, new()
{
_dbSet.RemoveRange(ids.Select(i => new T() { Id = i }).ToList());
}
or if the method is in a generic class, the method would look like
public void DeleteRange(int[] ids)
{
_dbSet.RemoveRange(ids.Select(i => new T() { Id = i }).ToList());
}
And there's no reason to mark this as Async now since it doesn't do any database access.

NET Core MongoDb Update/Replace exclude fields

I'm trying to complete a general repository for all of the entities in my application. I Have a BaseEntity with property Id, CreatorId and LastModifiedUserId. Now I'd like to Update a record in a collection, without having to modify the field CreatorId, so I have (from the client) an Entity valorized with some fields updated that I want to update.
Hi have 2 ways:
UpdateOneAsync
ReplaceOneAsync
The repo is created like this:
public class BaseRepository<T> : IRepository<T> where T : BaseEntity
{
public async Task<T> Replace/Update(T entity){...}
}
So it's very hard to use Update(1), since I should retrieve with reflection all the fields of T and exclude the ones that I don't want to update.
With Replace(2) I cannot find a way to specify which fields i should exclude when replacing an object with another. Projectionproperty in FindOneAndReplaceOptions<T>() just excludes the fields on the document that is returned after the update.
Am I missing a way in the replace method to exclude the fields or should I try to retrieve the fields with reflection and use a Update?
I don't know if this solution is ok for you .. what i do is :
Declare in Base Repo a method like
public virtual bool Update(TEntity entity, string key)
{
var result = _collection.ReplaceOne(x => x.Id.Equals(key), entity, new UpdateOptions
{
IsUpsert = false
});
return result.IsAcknowledged;
}
then in your controller when you want to update your entities is there where you set the prop you want to change .. like:
[HttpPut]
[ProducesResponseType(typeof(OrderDTO), 200)]
[ProducesResponseType(400)]
public async Task<ActionResult<bool>> Put([FromBody] OrderDTO value)
{
try
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var orderOnDb = await _orderService.FindAsync(xx => xx.Id == value.Id);
if (orderOnDb == null) return BadRequest(Constants.Error.NOT_FOUND_ON_MONGO);
// SET PROPERTY TO UPDATE (MANUALLY)
orderOnDb.LastUpdateDate = DateTime.Now;
orderOnDb.PaymentMethod = value.PaymentMethod;
orderOnDb.StateHistory = value.StateHistory;
//Save on db
var res = await _orderRepo.UpdateAsync(orderOnDb, orderOnDb.Id);
return res;
}
catch (Exception ex)
{
_logger.LogCritical(ex, ex.Message);
throw ex;
}
}
Hope it helps you!!!

Finally got Include working with child objects in Entity Framework 6.1

I'm using a Generic Repository pattern, so it was crucial that I got this to work. I read in a couple of places online that Entity Framework will start to ignore Includes once the shape of the query changes. The fix was to move the Includes to the end of the query. This did not work for me. In fact, just the exact opposite is working. I moved the Where statement to the end of the Includes query.
Here is what I had.
public Task<List<T>> ItemsWithAsync(Expression<Func<T, bool>> predicate = null, params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = null;
if (predicate != null)
query = _context.Set<T>().Where(predicate);
else
query = _context.Set<T>();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query.ToListAsync();
}
Here is what now works for me.
public Task<List<T>> ItemsWithAsync2(Expression<Func<T, bool>> predicate = null, params Expression<Func<T, object>>[] includeProperties)
{
var query = _context.Set<T>() as IQueryable<T>; // _dbSet = dbContext.Set<TEntity>()
query = includeProperties.Aggregate(query, (current, property) => current.Include(property)).Where(predicate);
return query.AsNoTracking().ToListAsync();
}
The key for me was to move the .Where(predicate) to the end of the query that handles all the includedProperties.
In my case, this returns all the Parent objects and two Child objects for each record. Prior to this fix, I would get all the Parents objects and only 4 of the records would contain the Child objects.
Here is how I'm calling the methods.
using (var uow = _unitOfWorkFactory.Create())
{
return (await uow.OfferRepository.ItemsWithAsync2(o =>
o.Deleted == false
&& o.StartDate <= clientDateTime
&& o.ExpiryDate >= clientDateTime, o => o.Merchant, o => o.App)).ToList();
}
Hope this helps! I searched for days and days. I never found the exact solution posted, which is why I posted it. Can anyone comment if this is an efficient way to solve the problem? Also, is the query.AsNoTracking actually needed? Thanks!
Here is what now works for me.
public Task<List<T>> ItemsWithAsync2(Expression<Func<T, bool>> predicate = null, params Expression<Func<T, object>>[] includeProperties)
{
var query = _context.Set<T>() as IQueryable<T>; // _dbSet = dbContext.Set<TEntity>()
query = includeProperties.Aggregate(query, (current, property) => current.Include(property)).Where(predicate);
return query.AsNoTracking().ToListAsync();
}
The key for me was to move the .Where(predicate) to the end of the query that handles all the includedProperties.
In my case, this returns all the Parent objects and two Child objects for each record. Prior to this fix, I would get all the Parents objects and only 4 of the records would contain the Child objects.
This also works with nested Child objects like o.Merchant.Locations.

Cascading Properties in Entity Framework via Repository Pattern

Say I have a class Person that has an "IsArchived" property. A Person can have a Collection - each of which can also be Archived.
Right now I have an UpdatePerson method in my repository that looks like this:
public Person UpdatePerson(Person person)
{
_db.Persons.Attach(person);
var entry = _db.Entry(person);
entry.Property(e => e.Name).IsModified = true;
entry.Property(e => e.Friends).IsModified = true;
SaveChanges();
return entry.Entity;
}
And I have a separate Repository method for Archive:
public void ArchivePerson(int personId)
{
var person = _db.Persons.FirstOrDefault(c => c.PersonId == personId);
if (person != null)
{
person.IsArchived = true;
foreach (var friend in person.Friends)
{
ArchivePerson(friend.PersonId);
}
SaveChanges();
}
}
This means that from my WebAPI, I have to dedicate a function to Archiving/Unarchiving. It would be cool if I could just call Update and set the Archived for the given entity, and apply this to all child entities where possible. Is there any way to accomplish this, or am I best off sticking to the ArchivePerson repository method?

Entity Framework 5. Multiple Include. Is this possible?

I am trying to create a multiple include method in my repository to use as follows:
repository.Include<Post>(x => x.Images, x => x.Tags).First(x => x.Id == 1)
I tried something as:
public IQueryable<T> Include<T>(params Expression<Func<T, Object>>[] paths) where T : class {
return paths.Aggregate(_context.Set<T>(), (x, path) => x.Include(path));
} // Include
But I get the error:
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Entity.DbSet'.
Note that the original include is the following:
public static IQueryable Include(
this IQueryable source,
Expression> path
) where T : class;
Can I make this work without turning my repository method into static?
Thank You,
Miguel
If you really want to create your own .Include non-extension method that allows for multiple paths in one call, internally translating to the already provided .Include method, you can do something like
public IQueryable<T> Include<T>(params Expression<Func<T, object>>[] paths)
where T : class
{
IQueryable<T> query = _context.Set<T>();
foreach (var path in paths)
query = query.Include(path);
return query;
}
This is pretty close to what you have already, but avoids the pitfall with Enumerable.Aggregate that you encountered: you'd have the same problem if you replace IQueryable<T> query with var query in my version.
Note that using many .Include may harm performance. If it does in your situation, you can rewrite it to use multiple queries, which you can run one after the other in a transaction.
Personally, as you can call the already provided .Include extension method (using System.Data.Entity;) on any IQueryable, I'd just write it as:
repository.Posts.Include(x => x.Images).Include(x => x.Tags).First(x => x.Id == 1)
public ICollection<SomeClass> Filter(string name, int skip, int take,out int total, params Expression<Func<SomeClass, object>>[] includeProperties) {
IQueryable<SomeClass> query = Session.All<SomeClass>().AsNoTracking();
//query = includeProperties.Aggregate(query, (current, property) => current.Include(property));
foreach (var property in includeProperties){
query = query.Include(property);
}
if (!string.IsNullOrWhiteSpace(name)){
query = query.Where(x => x.Name.Contains(name));
var page = query.OrderBy(x => x.Name)
.Skip(skip)
.Take(take)
.GroupBy(p => new{Total = query.Count()})
.FirstOrDefault();
total = (page != null) ? page.Key.Total : 0;
if (page == null) {
return new List<SomeClass>();
}
return page.ToList();
} else {
var page = query.OrderBy(x => x.Name)
.Skip(skip)
.Take(take)
.GroupBy(p => new { Total = query.Count() })
.FirstOrDefault();
total = (page != null) ? page.Key.Total : 0;
if (page == null) {
return new List<SomeClass>();
}
return page.ToList();
}
}
I guess the shortest way is as follows:
public static class LinqExtensions
{
/// <summary>
/// Acts similar of .Include() LINQ method, but allows to include several object properties at once.
/// </summary>
public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query, params Expression<Func<T, object>>[] paths)
where T : class
{
foreach (var path in paths)
query = query.Include(path);
return query;
}
}