NAVIGATION - SEARCH

Entity Framework - Code First DB migration

Code first is useful in Domain Driven Design.
Code first use Convention based migration- it is a set of rules to automatically configure a conceptual model based on domain class definition.

Type Discovery- Code first approach generates classes for the properties set in DBSet and also includes referenced types that are included in classes, even if referenced types are defined in another assembly.

The conventions for the type discovery are:

  1. Code-First includes types defined as a DbSet property in context class.
  2. Code-First includes reference types included in entity types even if they are defined in different assembly.
  3. Code-First includes derived classes even if only the base class is defined as DbSet property.
Assuming entities are created and DBContext is set along with the DB Connection string to enable migration use -

enable-migration command in package manager console.
once the migrations are enabled Migrations folder is created with the InitalCreate.cs class. Any further changes to the model a version of changes are created with a Up() and down() method which helps to Migrate and Rollback the model changes to database.

To update the last changes to the DB use add-migration "migration title" and update-database command in console.
This should now update or create Initial tables and changed schema. Along side this a Seed() method is called. You may need to add the initial/sample data to tables use the context for same in Seed() method.

To Rollback the changes use update-database -targetmigration "migration title". I find this a bit confusing initially but what this command does is it takes back the database state to the last best migration performed rollingback the latest changes performed.

While doing so you may receive error-
Unable to generate an explicit migration because the following explicit migrations are pending: [201704251318501_migrationname]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

This seems to complaining about the migration which are performed where a version is missing. So migrations has to be happen in a sequence of it was created.


Hope this helps.


Error whilst Reverse engineer code first using EF power tool

If you receive following error - change project Target Framework to 4.5.1.

One or more errors occurred while processing template 'Entity.tt'. error : An exception was thrown while trying to compile the transformation code. The following Exception was thrown: System.ArgumentException: Empty path name is not legal. at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.IO.File.OpenRead(String path) at Roslyn.Utilities.FileUtilities.OpenFileStream(String path) at Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(String path, MetadataReferenceProperties properties, DocumentationProvider documentation) at Microsoft.VisualStudio.TextTemplating.CompilerBridge.<>c.<.ctor>b__15_0(String x) at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext() at System.Linq.Enumerable.d__66`1.MoveNext() at System.Linq.Enumerable.d__66`1.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items) at Microsoft.CodeAnalysis.ImmutableArrayExtensions.AsImmutableOrEmpty[T](IEnumerable`1 items) at Microsoft.CodeAnalysis.Compilation.ValidateReferences[T](IEnumerable`1 references) at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.WithReferences(IEnumerable`1 references) at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CommonWithReferences(IEnumerable`1 newReferences) at Microsoft.VisualStudio.TextTemplating.CompilerBridge.PrepareNewCompilation() at Microsoft.VisualStudio.TextTemplating.CompilerBridge.Compile() at Microsoft.VisualStudio.TextTemplating.TransformationRunner.Compile(String source, String inputFile, IEnumerable`1 references, Boolean debug, SupportedLanguage language, String compilerOptions)

EF Power tool for VS2015

EF power tool downloadable from here currently are not compatible with VS 2015. To make it compatible perform following steps-
2. Rename file EFPowerTools.vsix to .zip
3. Open extension.vsixmanifest file and add following 




4. Save changes and Rename file to .vsix
5. Install tool

You should now be able to see the Entity Framework option on right click of your project.

Hope this helps