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

'The object has been removed from the model.' when building model #25138

Closed
ChristopherHaws opened this issue Jun 23, 2021 · 0 comments · Fixed by #25211
Closed

'The object has been removed from the model.' when building model #25138

ChristopherHaws opened this issue Jun 23, 2021 · 0 comments · Fixed by #25211
Labels
area-model-building 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

@ChristopherHaws
Copy link
Contributor

ChristopherHaws commented Jun 23, 2021

I am getting 'The object has been removed from the model.' when building a model. I have simplified my scenario down and found that this is happening when configuring the second owned type, each of which are the same .NET type, and the first owned type is using HasIndex.

Code to Reproduce

async Task Main() {
    var services = new ServiceCollection();

    services.AddDbContext<ApplicationContext>(db => {
        //db.UseSqlServer(@"Server=(localdb)\mssqllocaldb; Database=test; Trusted_Connection=True; MultipleActiveResultSets=true")
        //    .EnableDetailedErrors()
        //    .EnableSensitiveDataLogging();
        db.UseInMemoryDatabase(Guid.NewGuid().ToString());
    });

    using var root = services.BuildServiceProvider();
    using var scope = root.CreateScope();

    var db = scope.ServiceProvider.GetRequiredService<ApplicationContext>();
    //await db.Database.EnsureDeletedAsync();
    await db.Database.EnsureCreatedAsync();
}

public class ApplicationContext : DbContext {
    public ApplicationContext(DbContextOptions<ApplicationContext> options)
        : base(options) {
    }

    public DbSet<Author> Authors { get; set; } = default!;

    protected override void OnModelCreating(ModelBuilder model) {
        model.Entity<Author>(author => {    
            author.OwnsOne(x => x.PrimaryContact, primaryContact => {
                primaryContact.HasIndex(x => x.PhoneNumber);
            });

            author.OwnsOne(x => x.SecondaryContact);
        });
    }
}

public sealed class Author {
    public Guid AuthorId { get; init; } = default!;
    public Contact PrimaryContact { get; init; } = default!;
    public Contact SecondaryContact { get; init; }
}

public sealed class Contact {
    public string PhoneNumber { get; init; } = default!;
    public string EmailAddress { get; init; } = default!;
}

Stack Trace

System.InvalidOperationException: The object has been removed from the model.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.get_Builder()
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertiesSnapshot.Attach(InternalEntityTypeBuilder entityTypeBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.Snapshot.Attach(InternalEntityTypeBuilder entityTypeBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(TypeIdentity& type, ConfigurationSource configurationSource, Nullable`1 shouldBeOwned)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.SharedTypeEntity(String name, Type type, ConfigurationSource configurationSource, Nullable`1 shouldBeOwned)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalForeignKeyBuilder.MakeDeclaringTypeShared(Nullable`1 configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.GetTargetEntityTypeBuilder(TypeIdentity targetEntityType, MemberIdentity navigation, Nullable`1 configurationSource, Nullable`1 targetShouldBeOwned)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.HasOwnership(TypeIdentity& targetEntityType, MemberIdentity& navigation, Nullable`1 inverse, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.HasOwnership(TypeIdentity& typeIdentity, MemberIdentity navigation, ConfigurationSource configurationSource)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.OwnsOneBuilder[TRelatedEntity](TypeIdentity ownedType, MemberIdentity navigation)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.OwnsOne[TRelatedEntity](Expression`1 navigationExpression, Action`1 buildAction)
   at UserQuery.ApplicationContext.<>c.<OnModelCreating>b__5_0(EntityTypeBuilder`1 author) in C:\Users\cribs\AppData\Local\Temp\LINQPad6\_sxpcqoeo\udvecs\LINQPadQuery:line 38
   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity[TEntity](Action`1 buildAction)
   at UserQuery.ApplicationContext.OnModelCreating(ModelBuilder model) in C:\Users\cribs\AppData\Local\Temp\LINQPad6\_sxpcqoeo\udvecs\LINQPadQuery:line 33
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_Dependencies()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreatedAsync(CancellationToken cancellationToken)
   at UserQuery.Main() in C:\Users\cribs\AppData\Local\Temp\LINQPad6\_sxpcqoeo\udvecs\LINQPadQuery:line 19

Provider and version information

Regression: Works in 5.0.7
EF Core version: 6.0.0-preview.5.21301.9
Database provider: Microsoft.EntityFrameworkCore.SqlServer or Microsoft.EntityFrameworkCore.InMemoryDatabase
Target framework: .NET 6.0.100-preview.5.21302.13
Operating system: Windows 10
IDE: Visual Studio 2019, Visual Studio 2022, LinqPad

@AndriySvyryd AndriySvyryd self-assigned this Jun 25, 2021
@AndriySvyryd AndriySvyryd added this to the 6.0.0 milestone Jun 25, 2021
AndriySvyryd added a commit that referenced this issue Jul 6, 2021
Store Owned configuration on EntityType
Add methods to IMutableModel and IConventionModel for creating owned entity types
Mark entity types discovered from DbSet properties on DbContext as configured explicitly
Make sure ownerships have an associated principal to dependent navigation at all times

Fixes #25138
AndriySvyryd added a commit that referenced this issue Jul 7, 2021
Store Owned configuration on EntityType
Add methods to IMutableModel and IConventionModel for creating owned entity types
Mark entity types discovered from DbSet properties on DbContext as configured explicitly
Make sure ownerships have an associated principal to dependent navigation at all times
Store owned types in a field instead of an annotation

Fixes #25138
@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 Jul 7, 2021
@AndriySvyryd AndriySvyryd removed their assignment Jul 7, 2021
AndriySvyryd added a commit that referenced this issue Jul 9, 2021
Store Owned configuration on EntityType
Add methods to IMutableModel and IConventionModel for creating owned entity types
Make sure ownerships have an associated principal to dependent navigation at all times
Store owned types in a field instead of an annotation

Fixes #25138
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-preview7 Jul 28, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-preview7, 6.0.0 Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building 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.

4 participants