Repro - create the following model:
```
public abstract class Weapon
{
public int Id { get; set; }
// 1 - 1 self reference
public virtual Weapon SynergyWith { get; set; }
}
public class HeavyWeapon : Weapon
{
public bool Overheats { get; set; }
}
public class GearsOfWarContext : DbContext
{
public GearsOfWarContext()
{
Configuration.ValidateOnSaveEnabled = false;
}
public DbSet<Weapon> Weapons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Weapon>().HasOptional(w => w.SynergyWith).WithOptionalPrincipal();
}
}
```
Enable-Migrations
Add-Migration Mig1
Update-Database
now add the following line to OnModelCreating:
```
modelBuilder.Entity<Weapon>().MapToStoredProcedures();
```
Add-Migration Mig2 throws the following exception:
__Instances of abstract classes cannot be created.__
at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at System.Data.Entity.Core.Objects.DelegateFactory.CreateConstructor(Type type)
at System.Data.Entity.Core.Objects.DelegateFactory.GetConstructorDelegateForType(ClrEntityType clrType)
at System.Data.Entity.Core.Objects.ObjectContext.CreateObject[T]()
at System.Data.Entity.Internal.InternalContext.CreateObject[TEntity]()
at System.Data.Entity.Internal.Linq.InternalSet`1.Create()
at System.Data.Entity.Internal.Linq.InternalDbSet`1.Create()
at System.Data.Entity.Migrations.Infrastructure.ModificationCommandTreeGenerator.ChangeRelationshipStates(DbContext context, EntityType entityType, Object entity, EntityState state)
at System.Data.Entity.Migrations.Infrastructure.ModificationCommandTreeGenerator.<Generate>d__12`1.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateFunctionBody[TCommandTree](StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, Func`3 treeGenerator, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String functionName, String rowsAffectedParameterName)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateInsertFunctionBody(StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<BuildCreateProcedureOperations>d__cd.MoveNext()
at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source, ModelMetadata target, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.DbMigrator.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.MigrationScaffolder.Scaffold(String migrationName, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Scaffold(MigrationScaffolder scaffolder)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
```
public abstract class Weapon
{
public int Id { get; set; }
// 1 - 1 self reference
public virtual Weapon SynergyWith { get; set; }
}
public class HeavyWeapon : Weapon
{
public bool Overheats { get; set; }
}
public class GearsOfWarContext : DbContext
{
public GearsOfWarContext()
{
Configuration.ValidateOnSaveEnabled = false;
}
public DbSet<Weapon> Weapons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Weapon>().HasOptional(w => w.SynergyWith).WithOptionalPrincipal();
}
}
```
Enable-Migrations
Add-Migration Mig1
Update-Database
now add the following line to OnModelCreating:
```
modelBuilder.Entity<Weapon>().MapToStoredProcedures();
```
Add-Migration Mig2 throws the following exception:
__Instances of abstract classes cannot be created.__
at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at System.Data.Entity.Core.Objects.DelegateFactory.CreateConstructor(Type type)
at System.Data.Entity.Core.Objects.DelegateFactory.GetConstructorDelegateForType(ClrEntityType clrType)
at System.Data.Entity.Core.Objects.ObjectContext.CreateObject[T]()
at System.Data.Entity.Internal.InternalContext.CreateObject[TEntity]()
at System.Data.Entity.Internal.Linq.InternalSet`1.Create()
at System.Data.Entity.Internal.Linq.InternalDbSet`1.Create()
at System.Data.Entity.Migrations.Infrastructure.ModificationCommandTreeGenerator.ChangeRelationshipStates(DbContext context, EntityType entityType, Object entity, EntityState state)
at System.Data.Entity.Migrations.Infrastructure.ModificationCommandTreeGenerator.<Generate>d__12`1.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateFunctionBody[TCommandTree](StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, Func`3 treeGenerator, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String functionName, String rowsAffectedParameterName)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateInsertFunctionBody(StorageEntityTypeModificationFunctionMapping modificationFunctionMapping, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<BuildCreateProcedureOperations>d__cd.MoveNext()
at System.Linq.Enumerable.<SelectManyIterator>d__31`3.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source, ModelMetadata target, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.DbMigrator.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.MigrationScaffolder.Scaffold(String migrationName, Boolean ignoreChanges)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Scaffold(MigrationScaffolder scaffolder)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)