Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Edited Issue: EF Stored Procs :: mapping Many-Many relationship when one class is abstract throws [1246]

$
0
0
Create the following model:

```
public class Gear
{
public int Id { get; set; }
public virtual ICollection<Weapon> Weapons { get; set; }
}

public abstract class Weapon
{
public int Id { get; set; }
}

public class HeavyWeapon : Weapon
{
public bool Overheats { get; set; }
}

public class StandardWeapon : Weapon
{
}

public class GearsModel : DbContext
{
public DbSet<Gear> Gears { get; set; }
public DbSet<Weapon> Weapons { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Gear>().HasMany(g => g.Weapons).WithMany().MapToStoredProcedures();
}
}

```

Enable-Migrations
Add-Migration Mig1

This 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.<GenerateAssociation>d__0`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](StorageAssociationSetModificationFunctionMapping modificationFunctionMapping, Func`3 treeGenerator, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String rowsAffectedParameterName)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.GenerateInsertFunctionBody(StorageAssociationSetModificationFunctionMapping modificationFunctionMapping, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<BuildCreateProcedureOperations>d__d0.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)

I expect this to work, because all we need in order to populate the link table are entity keys, which abstract class does contain.

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>