For a few days now, I have been struggling with retrieving my entities from a repository (DbContext
).
I am trying to save all the entities in an atomic action. Thus, different entities together represent something of value to me. If all the entities are 'valid', then I can save them all to the database. Entity 'a' is already stored in my repository, and needs to be retrieved to 'validate' entity 'b'.
That's where the problem arises. My repository relies on the DbSet<TEntity>
class which works great with Linq2Sql (Include()
navigation properties e.g.). But, the DbSet<TEntity>
does not contain entities that are in the 'added' state.
So I have (as far as I know) two options:
- Use the
ChangeTracker
to see which entities are available and query them into a set based on theirEntityState
. - Use the
DbSet<TEntity>.Local
property.
The ChangeTracker
seems to involve some extra hard work to get it working in a way such that I can use Linq2Sql to Include()
navigation properties e.g.
The DbSet<TEntity>.Local
seems a bit weird to me. It might just be the name. I just read something that it is not performing very well (slower than DbSet<> itself). Not sure if that is a false statement.
Could somebody with significant EntityFramework experience shine some light on this? What's the 'wise' path to follow? Or am I seeing ghosts and should I always use the .Local
property?
Update with code examples:
An example of what goes wrong
public void AddAndRetrieveUncommittedTenant()
{
_tenantRepository = new TenantRepository(new TenantApplicationTestContext());
const string tenantName = "testtenant";
// Create the tenant, but not call `SaveChanges` yet until all entities are validated
_tenantRepository.Create(tenantName);
//
// Some other code
//
var tenant = _tenantRepository.GetTenants().FirstOrDefault(entity => entity.Name.Equals(tenantName));
// The tenant will be null, because I did not call save changes yet,
// and the implementation of the Repository uses a DbSet<TEntity>
// instead of the DbSet<TEntity>.Local.
Assert.IsNotNull(tenant);
// Can I safely use DbSet<TEntity>.Local ? Or should I play
// around with DbContext.ChangeTracker instead?
}
An example of how I want to use my Repository
In my Repository
I have this method:
public IQueryable<TEntity> GetAll()
{
return Context.Set<TEntity>().AsQueryable();
}
Which I use in business code in this fashion:
public List<Case> GetCasesForUser(User user)
{
return _repository.GetAll().
Where(@case => @case.Owner.EmailAddress.Equals(user.EmailAddress)).
Include(@case => @case.Type).
Include(@case => @case.Owner).
ToList();
}
That is mainly the reason why I prefer to stick to DbSet
like variables. I need the flexibility to Include
navigation properties. If I use the ChangeTracker
I retrieve the entities in a List
, which does not allow me to lazy load related entities at a later point in time.
If this is close to incomprehensible bullsh*t, then please let me know so that I can improve the question. I desperately need an answer.
Thx a lot in advance!