Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to apply migration: [...]the entity type is defined in shadow state[...] #2801

Closed
saxx opened this issue Aug 8, 2015 · 3 comments
Closed

Comments

@saxx
Copy link

saxx commented Aug 8, 2015

Unfortunately I have no clue what EF is trying to tell me with the exception. This is what I did:

  1. Upgraded my EF7-beta4 project to beta6
  2. Added a simple string property to an entity
  3. Created a migration via dnx . ef migration add
  4. Manually cleaned up the migrations Up and Down methods, because the automatic scaffolding tried to (re-)create all tables etc.
  5. Tried to apply the new migration via dnx . ef migration apply

The exception I get:

dnx . ef migration apply
Using context 'Db'.
Using database 'Wuzlstats' on server '(localdb)\MSSQLLocalDB'.
System.InvalidOperationException: The navigation property 'LeagueId' cannot be added to entity type 'Wuzlstats.Models.Ga
me' because the entity type is defined in shadow state and navigations properties cannot be added to shadow state.
   at Microsoft.Data.Entity.Metadata.EntityType.AddNavigation(String name, ForeignKey foreignKey, Boolean pointsToPrinci
pal)
   at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.Navigation(String navigationName, ForeignKey for
eignKey, Boolean pointsToPrincipal, ConfigurationSource configurationSource, Boolean canOverrideSameSource)
   at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.Navigation(String navigationName, ForeignKey for
eignKey, Boolean pointsToPrincipal, ConfigurationSource configurationSource)
   at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.Relationship(InternalEntityTypeBuilder principal
EntityTypeBuilder, InternalEntityTypeBuilder dependentEntityTypeBuilder, String navigationToPrincipalName, String naviga
tionToDependentName, IReadOnlyList`1 foreignKeyProperties, IReadOnlyList`1 principalProperties, ConfigurationSource conf
igurationSource, Nullable`1 isUnique, Nullable`1 isRequired, Func`2 onRelationshipAdding)
   at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.Relationship(InternalEntityTypeBuilder principal
EntityTypeBuilder, InternalEntityTypeBuilder dependentEntityTypeBuilder, String navigationToPrincipalName, String naviga
tionToDependentName, ConfigurationSource configurationSource, Nullable`1 isUnique, Boolean strictPrincipal)
   at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.Relationship(EntityType principalEntityType, Ent
ityType dependentEntityType, String navigationToPrincipalName, String navigationToDependentName, ConfigurationSource con
figurationSource, Nullable`1 isUnique, Boolean strictPrincipal)
   at Microsoft.Data.Entity.Metadata.Builders.EntityTypeBuilder.ReferenceBuilder(EntityType relatedEntityType, String na
vigationName)
   at Microsoft.Data.Entity.Metadata.Builders.EntityTypeBuilder.Reference(String relatedTypeName, String navigationName)

   at Wuzlstats.Migrations.Initial.<>c.<BuildTargetModel>b__6_4(EntityTypeBuilder b) in C:\Users\saxx\Documents\Wuzlst
ats\src\Wuzlstats\Migrations\20150519184700_Initial.Designer.cs:line 85
   at Microsoft.Data.Entity.ModelBuilder.Entity(String name, Action`1 buildAction)
   at Wuzlstats.Migrations.Initial.BuildTargetModel(ModelBuilder builder) in C:\Users\saxx\Documents\Wuzlstats\src\Wuz
lstats\Migrations\20150519184700_Initial.Designer.cs:line 83
   at Microsoft.Data.Entity.Migrations.Infrastructure.MigrationModelFactory.Create(Action`1 onModelCreating)
   at Microsoft.Data.Entity.Migrations.Migrator.ApplyMigration(Migration migration)
   at Microsoft.Data.Entity.Migrations.Migrator.ApplyMigrations(String targetMigration)
   at Microsoft.Data.Entity.Commands.MigrationTool.ApplyMigration(String migrationName, String contextTypeName, String s
tartupAssemblyName)
   at Microsoft.Data.Entity.Commands.Program.<>c__DisplayClass13_0.<ApplyMigration>b__0()
   at Microsoft.Data.Entity.Commands.Program.Execute(String startupProject, Func`1 invoke)
   at Microsoft.Data.Entity.Commands.Program.ApplyMigration(String migration, String context, String startupProject)
   at Microsoft.Data.Entity.Commands.Program.<>c__DisplayClass10_3.<Main>b__9()
   at Microsoft.Framework.Runtime.Common.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.Data.Entity.Commands.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Framework.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider s
erviceProvider)
   at Microsoft.Framework.ApplicationHost.Program.ExecuteMain(DefaultHost host, String applicationName, String[] args)
   at Microsoft.Framework.ApplicationHost.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Framework.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider s
erviceProvider)
   at dnx.host.Bootstrapper.RunAsync(List`1 args, IRuntimeEnvironment env, FrameworkName targetFramework)
   at dnx.host.RuntimeBootstrapper.ExecuteAsync(String[] args, FrameworkName targetFramework)
   at dnx.host.RuntimeBootstrapper.Execute(String[] args, FrameworkName targetFramework)

Everything works fine when I create a new database from the model, only trying to migrate an existing database fails.

The affected code is on https://github.com/saxx/Wuzlstats/, please see this branch for the exact changes I did: https://github.com/saxx/Wuzlstats/compare/master...Timeouts?expand=1

What is a "shadow state", what is this exception trying to tell me, and how can I fix it? Thanks!

@bricelam
Copy link
Contributor

Migrations don't upgrade very well between pre-releases. There's been a lot of code churn in that area recently. Try deleting all your migrations, the model snapshot, and your database, then add a new migration and apply.

@saxx
Copy link
Author

saxx commented Aug 10, 2015

Thanks! Not really happy with that, because I need to drop my existing database and create a new one (and move all existing data to the new one), but I guess that comes with using a preview :)
Creating a new database works fine, by the way.

@saxx saxx closed this as completed Aug 10, 2015
@bricelam
Copy link
Contributor

Sorry, my suggestion to delete the database assumed that the data didn't really matter since EF7 is still very much a prerelease. 😅

If the schema hasn't changed, you could manually insert into the _MigrationHistory table to mark the new migration as applied.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants