NSubstitute DbSet / IQueryable

This happens because of NSubstitute syntax specific. For example in: ((IQueryable<Blog>) mockSet).Provider.Returns(data.Provider); NSubstitute calls the Provider’s getter, then it specifies the return value. This getter call isn’t intercepted by the substitute and you get an exception. It happens because of explicit implementation of IQueryable.Provider property in DbQuery class. You can explicitly create substitutes for multiple … Read more

DbContext AutoDetectChangesEnabled set to false detecting changes

Setting AutoDetectChangesEnabled to false doesn’t disable change tracking. (That’s what the AsNoTracking() extension method would do.) It just disables the automatic call of DetectChanges that would otherwise occur in many DbContext API methods. But DetectChanges isn’t the only method that participates in change tracking. However, if you don’t call it manually at the right places … Read more

How to convert DbSet in Entity framework to ObjectQuery

I found the answer. Of course, it is possible to convert DbSet in Entity framework to ObjectQuery using the below lines of code. ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext; ObjectSet<Request> objectSet = objectContext.CreateObjectSet<Request>(“Requests”); where, db – Context class inherting from DbContext. Requests – DbSet<Request> defined in Context class. objectSet – Can now be passed as ObjectQuery.

What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added?

When you use dbContext.SomeEntitySet.Add(entityInstance); the status for this and all its related entities/collections is set to added, while dbContext.Entry(entityInstance).State = EntityState.Added; adds also all the related entities/collections to the context but leaves them as unmodified. So if the entity that you are trying to create has a related entity (and it’s value its not null), … Read more

What is the best practice in EF Core for using parallel async calls with an Injected DbContext?

Using any context.XyzAsync() method is only useful if you either await the called method or return control to a calling thread that’s doesn’t have context in its scope. A DbContext instance isn’t thread-safe: you should never ever use it in parallel threads. Which means, just for sure, never use it in multiple threads anyway, even … Read more

How to Refresh DbContext

I just found that the Enumerable result should be evaluated because the Refresh method gets it as object and doesn’t evaluate it. var context = ((IObjectContextAdapter)myDbContext).ObjectContext; var refreshableObjects = (from entry in context.ObjectStateManager.GetObjectStateEntries( EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged) where entry.EntityKey != null select entry.Entity).ToList(); context.Refresh(RefreshMode.StoreWins, refreshableObjects); And I prefer the following: var refreshableObjects … Read more

EF Code First DBContext and Transactions

Yes. SaveChanges uses transaction internally. Use TransactionScope to wrap multiple calls to SaveChanges Example: using(var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { // Do something context.SaveChanges(); // Do something else context.SaveChanges(); scope.Complete(); }