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(); }

Using Entity Framework 6 with Multiple DB Schemas but using One DBContext

You can map each table to its own schema by fluent mapping only. In your DbContext subtype you should override OnModelCreating (if you haven’t done so already) and add statements like this: modelBuilder.Entity<Department>() .ToTable(“t_Department”, “school”); Entities that you don’t map like this explicitly will be placed in the default dbo schema, or you can provide … 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

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

DbContext discard changes without disposing

public void RejectChanges() { foreach (var entry in ChangeTracker.Entries()) { switch (entry.State) { case EntityState.Modified: case EntityState.Deleted: entry.State = EntityState.Modified; //Revert changes made to deleted entity. entry.State = EntityState.Unchanged; break; case EntityState.Added: entry.State = EntityState.Detached; break; } } } Update: Some users suggest to add .ToList() to avoid ‘collection was modified’ exception. But I believe … Read more

How to set CommandTimeout for DbContext?

It will work with your method. Or subclass it (from msdn forum) public class YourContext : DbContext { public YourContext() : base(“YourConnectionString”) { // Get the ObjectContext related to this DbContext var objectContext = (this as IObjectContextAdapter).ObjectContext; // Sets the command timeout for all the commands objectContext.CommandTimeout = 120; } }

c# entity framework: correct use of DBContext class inside your repository class

I think you should not follow the first article, and I will tell you why. Following the second approach you’re loosing pretty much every feature that Entity Framework provides via the DbContext, including its 1st-level cache, its identity map, its unit-of-work, and its change tracking and lazy-loading abilities. That’s because in the scenario above, a … Read more

Entity Framework 5 deep copy/clone of an entity

One cheap easy way of cloning an entity is to do something like this: var originalEntity = Context.MySet.AsNoTracking() .FirstOrDefault(e => e.Id == 1); Context.MySet.Add(originalEntity); Context.SaveChanges(); the trick here is AsNoTracking() – when you load an entity like this, your context do not know about it and when you call SaveChanges, it will treat it like … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)