‘Unable to resolve service for type ¨Microsoft.entityFrameworkCore.DbContextOptions¨1[LibraryData.LibraryContext] while attempting to activate

I fixed this error by specifying –project and –startup-project options to Entity Framework Core CLI tools like this: dotnet ef database update –verbose –project CommandService.Data –startup-project CommandService –project means which project contains the DbContext class –startup-project means which project contains the database connection information and other information.

EF (entity framework) usage of “using” statement

I think you will find many suggesting this style of pattern. Not just me or Henk DBContext handling Yes, Ideally Using statements for DBContext subtypes Even better Unit Of Work patterns that are managed with Using, that have a context and dispose the context Just 1 of many UoW examples, this one from Tom Dykstra … Read more

Removing many to many entity Framework

Standard way is to load the artist including the current related types from the database and then remove the types with the selected Ids from the loaded types collection. Change tracking will recognize which types have been removed and write the correct DELETE statements to the join table: var artist = this._db.Artists.Include(a => a.ArtistTypes) .SingleOrDefault(a … Read more

Configure multiple database Entity Framework 6

It’s not important that how many DbContexts you have(In entity framework 6). Just put connection strings in appConfig or webConfig of startup project. Then you’re ready to go. Example of appConfig with two connectionString with Ef 6.01 & Sql Compact 4.0 <configSections> <section name=”entityFramework” type=”System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ requirePermission=”false” /> </configSections> <connectionStrings> <add name=”MainDb” … Read more

unexpected GetType() result for entity entry

You can get the original entity type of a proxy type by ObjectContext.GetObjectType(entity.GetType()) This is a static method of ObjectContext, so you can readily use in in a DbContext environment. If for some reason you need the actual entity as its original type you can use the pattern var entity = entry.Entity as MyEntity; if … Read more

How can I log all entities change, during .SaveChanges() using EF code first?

You can get the before and after values for all changed entities by going through DbContext.ChangeTracker. Unfortunately the API is a little verbose: var changeInfo = context.ChangeTracker.Entries() .Where (t => t.State == EntityState.Modified) .Select (t => new { Original = t.OriginalValues.PropertyNames.ToDictionary (pn => pn, pn => t.OriginalValues[pn]), Current = t.CurrentValues.PropertyNames.ToDictionary (pn => pn, pn => … Read more

Why re-initiate the DbContext when using the Entity Framework?

Managing Lifetime You’re correct that a single static instance of DbContext is usually not recommended: The more you use an ObjectContext, generally the bigger it gets. This is because it holds a reference to all the Entities it has ever known about, essentially whatever you have queried, added or attached. So you should reconsider sharing … Read more

DbSet.Find method ridiculously slow compared to .SingleOrDefault on ID

Find calls DetectChanges internally, SingleOrDefault (or generally any query) doesn’t. DetectChanges is an expensive operation, so that’s the reason why Find is slower (but it might become faster if the entity is already loaded into the context because Find would not run a query but just return the loaded entity). If you want to use … Read more