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

NullReferenceException in ForeignKeyPropertyDiscoveryConvention.Apply #6867

Closed
HappyNomad opened this issue Oct 26, 2016 · 0 comments
Closed
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@HappyNomad
Copy link

I just upgraded to 1.1 Preview 1 and then encountered this exception.

Trying to create a database using this context and model will reproduce the issue:

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

    public DbSet<A> As { get; set; }
}

public class A
{
    public int ID { get; set; }
    public L L1 { get; set; }
    public IList<PBase> L2 { get; set; }
}

public class L
{
    public int ID { get; set; }
    public IList<T> Ts { get; set; }
}

public class T : P
{
    public Q D { get; set; }
    public Q P1 { get; set; }
    public P P2 { get; set; }
}

public class P : PBase { }

public class Q : PBase { }

public abstract class PBase
{
    public int ID { get; set; }
    public string Stuff { get; set; }
}

Note that this model is almost the same as my second example in #6792. The only difference is the properties (or lack thereof) on T. Here has the same strange workaround of flipping the L1 and L2 names to make the exception disappear.

Here's the full exception text:

System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Microsoft.EntityFrameworkCore
StackTrace:
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ForeignKeyPropertyDiscoveryConvention.Apply(InternalRelationshipBuilder relationshipBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ForeignKeyPropertyDiscoveryConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder, Key key)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnKeyRemoved(InternalEntityTypeBuilder entityTypeBuilder, Key key)
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.RemoveKey(Key key, Boolean runConventions)
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.RemoveKey(IReadOnlyList1 properties, Boolean runConventions) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.RemoveKey(Key key, ConfigurationSource configurationSource, Boolean runConventions) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.RemoveKeyIfUnused(Key key) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.RemoveForeignKey(ForeignKey foreignKey, ConfigurationSource configurationSource, Boolean canOverrideSameSource, Boolean runConventions) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.RemoveForeignKey(ForeignKey foreignKey, List1 removedNavigations, List1 removedForeignKeys) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.GetOrCreateRelationshipBuilder(EntityType principalEntityType, EntityType dependentEntityType, Nullable1 navigationToPrincipal, Nullable1 navigationToDependent, IReadOnlyList1 dependentProperties, IReadOnlyList1 principalProperties, Nullable1 isRequired, Boolean removeCurrent, Nullable1 principalEndConfigurationSource, Nullable1 configurationSource, List1 removedNavigations, List1 removedForeignKeys, List1 addedForeignKeys, Nullable1& existingRelationshipInverted)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.ReplaceForeignKey(InternalEntityTypeBuilder principalEntityTypeBuilder, InternalEntityTypeBuilder dependentEntityTypeBuilder, Nullable1 navigationToPrincipal, Nullable1 navigationToDependent, IReadOnlyList1 dependentProperties, IReadOnlyList1 principalProperties, Nullable1 isUnique, Nullable1 isRequired, Nullable1 deleteBehavior, Boolean removeCurrent, Boolean oldRelationshipInverted, Nullable1 principalEndConfigurationSource, Nullable1 configurationSource, Boolean runConventions, List1 removedNavigations, List1 removedForeignKeys) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.ReplaceForeignKey(Nullable1 configurationSource, InternalEntityTypeBuilder principalEntityTypeBuilder, InternalEntityTypeBuilder dependentEntityTypeBuilder, Nullable1 navigationToPrincipal, Nullable1 navigationToDependent, IReadOnlyList1 dependentProperties, IReadOnlyList1 principalProperties, Nullable1 isUnique, Nullable1 isRequired, Nullable1 deleteBehavior, Boolean removeCurrent, Nullable1 principalEndConfigurationSource, Boolean oldRelationshipInverted, Boolean runConventions)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.HasForeignKey(IReadOnlyList1 properties, EntityType dependentEntityType, Nullable1 configurationSource, Boolean runConventions)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.HasForeignKey(IReadOnlyList1 properties, EntityType dependentEntityType, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalRelationshipBuilder.HasForeignKey(IReadOnlyList1 properties, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ForeignKeyPropertyDiscoveryConvention.Apply(InternalRelationshipBuilder relationshipBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ForeignKeyPropertyDiscoveryConvention.Apply(InternalPropertyBuilder propertyBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnPropertyAdded(InternalPropertyBuilder propertyBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddProperty(String name, Type propertyType, MemberInfo memberInfo, ConfigurationSource configurationSource, Boolean runConventions)
at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.AddProperty(MemberInfo memberInfo, ConfigurationSource configurationSource, Boolean runConventions)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(Property existingProperty, String propertyName, Type propertyType, MemberInfo clrProperty, Nullable1 configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(String propertyName, Type propertyType, MemberInfo clrProperty, Nullable1 configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.Property(MemberInfo clrProperty, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyDiscoveryConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyDiscoveryConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder, EntityType oldBaseType)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnBaseEntityTypeSet(InternalEntityTypeBuilder entityTypeBuilder, EntityType previousBaseType)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.HasBaseType(EntityType baseEntityType, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.BaseTypeDiscoveryConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnEntityTypeAdded(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationshipDiscoveryConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnEntityTypeAdded(InternalEntityTypeBuilder entityTypeBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType(EntityType entityType, Boolean runConventions)
at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType(Type type, ConfigurationSource configurationSource, Boolean runConventions)
at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalModelBuilder.Entity(Type type, ConfigurationSource configurationSource, Boolean runConventions)
at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(Type type)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.FindSets(ModelBuilder modelBuilder, DbContext context)
at Microsoft.EntityFrameworkCore.Infrastructure.Internal.RelationalModelSource.FindSets(ModelBuilder modelBuilder, DbContext context)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass14_0.b__0(Object k)
at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.LazyRef1.get_Value() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_6(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactoryService(FactoryService factoryService, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(IServiceCallSite callSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.Storage.DatabaseProviderServices.GetService[TService]() at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerDatabaseProviderServices.get_RelationalDatabaseCreator() at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseProviderServices.get_Creator() at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServiceCollectionExtensions.<>c.<AddEntityFramework>b__0_13(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactoryService(FactoryService factoryService, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(IServiceCallSite callSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
at EFGetStarted.AspNetCore.NewDb.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, BloggingContext dataContext) in C:\Users\Adrian\Development\Examples\ASP.NET Core\EFGetStarted.AspNetCore.NewDb\src\EFGetStarted.AspNetCore.NewDb\Startup.cs:line 56
InnerException:

@rowanmiller rowanmiller added this to the 1.1.0 milestone Oct 26, 2016
AndriySvyryd added a commit that referenced this issue Oct 27, 2016
…rtyDiscoveryConvention

Make InternalEntityTypeBuilder.HasKey more resilient by removing conflicting fks of lower source
Reset FieldInfo when lifting properties if the field doesn't exist on the new type
Improve the exception message for removing a property that is still being referenced

Fixes #6867
AndriySvyryd added a commit that referenced this issue Oct 27, 2016
…rtyDiscoveryConvention

Make InternalEntityTypeBuilder.HasKey more resilient by removing conflicting fks of lower source
Reset FieldInfo when lifting properties if the field doesn't exist on the new type
Improve the exception message for removing a property that is still being referenced

Fixes #6867
AndriySvyryd added a commit that referenced this issue Nov 3, 2016
…rtyDiscoveryConvention

Make InternalEntityTypeBuilder.HasKey more resilient by removing conflicting fks of lower source
Reset FieldInfo when lifting properties if the field doesn't exist on the new type
Improve the exception message for removing a property that is still being referenced

Fixes #6867
@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 Nov 3, 2016
@AndriySvyryd AndriySvyryd removed their assignment Nov 3, 2016
smitpatel pushed a commit that referenced this issue Nov 8, 2016
…rtyDiscoveryConvention

Make InternalEntityTypeBuilder.HasKey more resilient by removing conflicting fks of lower source
Reset FieldInfo when lifting properties if the field doesn't exist on the new type
Improve the exception message for removing a property that is still being referenced

Fixes #6867
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. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants