efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Exception in model building algorithm with some special property

Open chandrush opened this issue 10 years ago • 5 comments

Hello!

I've met strange error at adding migration with entity (dryed for example)

public class IssuerEntity
{
    public string Id { get; set; }

    public X509Certificate2 Certificate
    {
        get
        {
            return null;
            //return new X509Certificate2(CertificateBytes);
        }
    }
}

and ModelCreating code

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var ie = modelBuilder.Entity<IssuerEntity>();
    ie.Ignore(e => e.Certificate);

    ie.Property(c => c.Id)
        .HasMaxLength(32);
    ie.Property(e => e.Id);
}

If I comment IssuerEntity.Certificate property - migration is added, as expected

The error is

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.IsIgnored(String name, ConfigurationSource configurationSource) at Microsoft.Data.Entity.Metadata.Conventions.Internal.RelationshipDiscoveryConvention.FindRelationshipCandidates(InternalEntityTypeBuilder entityTypeBuilder) at Microsoft.Data.Entity.Metadata.Conventions.Internal.RelationshipDiscoveryConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder) at Microsoft.Data.Entity.Metadata.Conventions.Internal.RelationshipDiscoveryConvention.ApplyOnRelatedEntityTypes(InternalModelBuilder modelBuilder, EntityType entityType) at Microsoft.Data.Entity.Metadata.Conventions.Internal.RelationshipDiscoveryConvention.Apply(InternalEntityTypeBuilder entityTypeBuilder, EntityType oldBaseType) at Microsoft.Data.Entity.Metadata.Conventions.Internal.ConventionDispatcher.OnBaseEntityTypeSet(InternalEntityTypeBuilder entityTypeBuilder, EntityType previousBaseType) at Microsoft.Data.Entity.Metadata.Internal.InternalEntityTypeBuilder.HasBaseType(EntityType baseEntityType, ConfigurationSource configurationSource) at Microsoft.Data.Entity.Metadata.Internal.InternalModelBuilder.Remove(EntityType entityType, ConfigurationSource configurationSource) at Microsoft.Data.Entity.Metadata.Internal.InternalModelBuilder.Ignore(EntityType entityType, String name, ConfigurationSource configurationSource) at Microsoft.Data.Entity.Metadata.Internal.InternalModelBuilder.RemoveEntityTypesUnreachableByNavigations(ConfigurationSource configurationSource) at Microsoft.Data.Entity.Metadata.Conventions.Internal.ModelCleanupConvention.Apply(InternalModelBuilder modelBuilder) at Microsoft.Data.Entity.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder) at Microsoft.Data.Entity.ModelBuilder.Validate() at Microsoft.Data.Entity.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at Microsoft.Data.Entity.Internal.DbContextServices.CreateModel() at Microsoft.Data.Entity.Internal.LazyRef`1.get_Value() at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetService[T](IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.Data.Entity.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.Data.Entity.Commands.Program.Executor.<>c__DisplayClass6_0.b__0() at Microsoft.Data.Entity.Commands.Program.Executor.Execute(Action action) Object reference not set to an instance of an object.

chandrush avatar Mar 21 '16 16:03 chandrush

Version of EF?

smitpatel avatar Mar 21 '16 17:03 smitpatel

The version of EF is 7.0.0-rc1-final.

chandrush avatar Mar 21 '16 19:03 chandrush

dupe of #3865 It was appearing in RC1 but due code changes afterwards it doesn't repro anymore.

smitpatel avatar Mar 21 '16 20:03 smitpatel

If I will turn Certificate property into private and comment a line "ie.Ignore(e => e.Certificate)", I will still meet the error. If this is the same bug, then ok.

chandrush avatar Mar 22 '16 06:03 chandrush

As of RC1 private properties are still added to the model by convention. If you comment the line with Ignore then it will try to bring the type X509Certificate2 as an entity type. That could possibly bring a lot of entity types from references within that class. If anyone of those cant be assigned PK conventionally then exception will be thrown. If you can get exact repro I can try running it against the latest code to verify if its the same bug.

smitpatel avatar Mar 22 '16 06:03 smitpatel