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

2.2.6 migrations fail after upgrade to 3.0.0 if model has nested owned types #18183

Closed
mcetkovsky opened this issue Oct 2, 2019 · 6 comments · Fixed by #18549
Closed

2.2.6 migrations fail after upgrade to 3.0.0 if model has nested owned types #18183

mcetkovsky opened this issue Oct 2, 2019 · 6 comments · Fixed by #18549
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@mcetkovsky
Copy link

mcetkovsky commented Oct 2, 2019

Add-Migration fails after upgrading a project from 2.2.6 to 3.0.0 if the project contains nested owned entities.

Steps to reproduce

What steps can we follow to reproduce the issue?

  1. Open the attached solution with a .NET Core 2.2 project
  2. Run Add-Migration InitialMigration
  3. Upgrade the project to .NET Core 3.0
    • Edit EFCoreOwnTypesTest.csproj and replace netcoreapp2.2 with netcoreapp3.0 and all 2.2.6 with 3.0.0
    • Edit OrderConfiguration.cs and replace all ReferenceOwnershipBuilder with OwnedNavigationBuilder
    • Save changes in both files
  4. Run Add-Migration -v AfterUpgrade

The second Add-Migration call fails complaining that
The type 'EFCoreOwnTypesTest.Buyer' cannot be configured as non-owned because an owned entity type with the same name already exists..
(stack trace bellow)

Further steps shows that the exception is not thrown if migrations are generated directly using .NET Core 3.0, i.e. the exception is specific to upgrade scenarios only. However, there seems to be an additional cache-like issue affecting the steps.

  1. Delete the Migrations folder so the project is again without any migrations
  2. Run Add-Migration -v CleanMigration

The third Add-Migration call fails complaining that
The type 'EFCoreOwnTypesTest.Buyer' cannot be configured as non-owned because an owned entity type with the same name already exists..
(stack trace bellow)
Remember, the project is now based on .NET Core 3.0

  1. Close Visual Studio
  2. Start Visual Studio and reopen the solution
  3. Run Add-Migration -v CleanMigration

The Add-Migration command now succeeds even the code and the configuration is the same as when performing the step 6.

I'm not sure whether this step 6 vs step 9 difference in results is a separate issue or not.

Stack trace

PM> Add-Migration -v AfterUpgrade
Using project 'EFCoreOwnTypesTest'.
Using startup project 'EFCoreOwnTypesTest'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile c:\Users\user\Repos\EFCoreOwnTypesTest\bin\Debug\netcoreapp3.0\EFCoreOwnTypesTest.deps.json --additionalprobingpath C:\Users\alik\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig c:\Users\user\Repos\EFCoreOwnTypesTest\bin\Debug\netcoreapp3.0\EFCoreOwnTypesTest.runtimeconfig.json C:\Users\user\.nuget\packages\microsoft.entityframeworkcore.tools\3.0.0\tools\netcoreapp2.0\any\ef.dll migrations add AfterUpgrade --json --verbose --no-color --prefix-output --assembly c:\Users\user\Repos\EFCoreOwnTypesTest\bin\Debug\netcoreapp3.0\EFCoreOwnTypesTest.dll --startup-assembly c:\Users\user\Repos\EFCoreOwnTypesTest\bin\Debug\netcoreapp3.0\EFCoreOwnTypesTest.dll --project-dir c:\Users\alik\Repos\EFCoreOwnTypesTest\ --language C# --working-dir C:\Users\user\Repos\EFCoreOwnTypesTest --root-namespace EFCoreOwnTypesTest
Using assembly 'EFCoreOwnTypesTest'.
Using startup assembly 'EFCoreOwnTypesTest'.
Using application base 'c:\Users\user\Repos\EFCoreOwnTypesTest\bin\Debug\netcoreapp3.0'.
Using working directory 'c:\Users\user\Repos\EFCoreOwnTypesTest'.
Using root namespace 'EFCoreOwnTypesTest'.
Using project directory 'c:\Users\user\Repos\EFCoreOwnTypesTest\'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'MyDbContextDesignFactory'.
Found DbContext 'MyDbContext'.
Finding application service provider...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Using DbContext factory 'MyDbContextDesignFactory'.
Using context 'MyDbContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding design-time services referenced by assembly 'EFCoreOwnTypesTest'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'EFCoreOwnTypesTest'...
No design-time services were found.
'MyDbContext' disposed.
System.InvalidOperationException: The type 'EFCoreOwnTypesTest.Buyer' cannot be configured as non-owned because an owned entity type with the same name already exists.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(TypeIdentity& type, ConfigurationSource configurationSource, Nullable`1 shouldBeOwned)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(String name, ConfigurationSource configurationSource, Nullable`1 shouldBeOwned)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationBuilder.FindRelatedEntityType(String relatedTypeName, String navigationName)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationBuilder.HasOne(String relatedTypeName, String navigationName)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationBuilder.HasOne(String navigationName)
   at EFCoreOwnTypesTest.Migrations.MyDbContextModelSnapshot.<>c.<BuildModel>b__0_3(OwnedNavigationBuilder b2) in c:\Users\user\Repos\EFCoreOwnTypesTest\Migrations\MyDbContextModelSnapshot.cs:line 62
   at Microsoft.EntityFrameworkCore.Metadata.Builders.OwnedNavigationBuilder.OwnsOne(String ownedTypeName, String navigationName, Action`1 buildAction)
   at EFCoreOwnTypesTest.Migrations.MyDbContextModelSnapshot.<>c.<BuildModel>b__0_2(OwnedNavigationBuilder b1) in c:\Users\user\Repos\EFCoreOwnTypesTest\Migrations\MyDbContextModelSnapshot.cs:line 49
   at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder.OwnsOne(String ownedTypeName, String navigationName, Action`1 buildAction)
   at EFCoreOwnTypesTest.Migrations.MyDbContextModelSnapshot.<>c.<BuildModel>b__0_1(EntityTypeBuilder b) in c:\Users\user\Repos\EFCoreOwnTypesTest\Migrations\MyDbContextModelSnapshot.cs:line 34
   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(String name, Action`1 buildAction)
   at EFCoreOwnTypesTest.Migrations.MyDbContextModelSnapshot.BuildModel(ModelBuilder modelBuilder) in c:\Users\user\Repos\EFCoreOwnTypesTest\Migrations\MyDbContextModelSnapshot.cs:line 32
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSnapshot.CreateModel()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSnapshot.get_Model()
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The type 'EFCoreOwnTypesTest.Buyer' cannot be configured as non-owned because an owned entity type with the same name already exists.

Further technical details

EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.0
Operating system: Windows 10 1903
IDE: Visual Studio 2019 16.3.2
EFCoreOwnTypesTest.zip

@dandenton
Copy link

It's probably not the best workaround but after getting that error I found the v2.2.0 HasOne() line referenced in the ModelSnapshot.cs file for the owned type (found from the stack trace) and replaced it manually with the v3.0 WithOwner(). After that Add-Migration worked. To fix the attached example you'd change line 62 from

b2.HasOne("EFCoreOwnTypesTest.Buyer")
	.WithOne("Address")
	.HasForeignKey("EFCoreOwnTypesTest.Address", "BuyerOrderId")
	.OnDelete(DeleteBehavior.Cascade);

to

b2.WithOwner()
	.HasForeignKey("BuyerOrderId");

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Oct 3, 2019

@dandenton This is by design and that's the best workaround.

@AndriySvyryd AndriySvyryd added the closed-no-further-action The issue is closed and no further action is planned. label Oct 3, 2019
@dandenton
Copy link

@AndriySvyryd Should the breaking changes document be updated to state that all existing migrations also need to be updated and not just the current configuration? The way I read the document was that only your current configuration would need to change. I assumed that a migration's Designer.cs file would ever need to be manually updated. Is there no way to use the legacy model building using the product version stored with the migration's snapshot?

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Oct 3, 2019

@mcetkovsky I forgot to mention that when upgrading to 3.0 you need to close and reopen VS to load the new EF tools
@dandenton Are you saying that old migrations don't work even after closing and reloading VS?

@AndriySvyryd AndriySvyryd reopened this Oct 3, 2019
@AndriySvyryd AndriySvyryd removed the closed-no-further-action The issue is closed and no further action is planned. label Oct 3, 2019
@AndriySvyryd AndriySvyryd changed the title Unable to add migration after upgrade from 2.2.6 to 3.0.0 if nested owned entities are present 2.2.6 migrations fail after upgrade to 3.0.0 if model has owned types with inverse navigation Oct 3, 2019
@dandenton
Copy link

Yes. The error always occurs even after an update/restart. It also happens when you script migrations using the 3.0 tool against migrations created with version 2.2.6-servicing-10079.

@AndriySvyryd AndriySvyryd changed the title 2.2.6 migrations fail after upgrade to 3.0.0 if model has owned types with inverse navigation 2.2.6 migrations fail after upgrade to 3.0.0 if model has nested owned types with inverse navigation Oct 3, 2019
@AndriySvyryd AndriySvyryd self-assigned this Oct 3, 2019
@mcetkovsky
Copy link
Author

mcetkovsky commented Oct 3, 2019

@AndriySvyryd The 3.0 tools seemed to be loaded even without restarting VS. However, they were still using no-longer-present deleted files from the deleted Migrations folder until the VS was restarted.

@ajcvickers ajcvickers added this to the 3.1.0 milestone Oct 7, 2019
@AndriySvyryd AndriySvyryd changed the title 2.2.6 migrations fail after upgrade to 3.0.0 if model has nested owned types with inverse navigation 2.2.6 migrations fail after upgrade to 3.0.0 if model has nested owned types Oct 12, 2019
AndriySvyryd added a commit that referenced this issue Oct 23, 2019
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 23, 2019
@AndriySvyryd AndriySvyryd removed their assignment Oct 23, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0, 3.1.0-preview2 Oct 24, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0-preview2, 3.1.0 Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants