1

Closed

EF Configuration Cause Null Reference Exception.

description

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ModelContext Context = new ModelContext("ConnectionString"))
            {
                var Data = Context.Data.ToList();
            }

        }
    }
    public partial class ModelContext : DbContext
    {
        public ModelContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            Database.SetInitializer<ModelContext>(null);
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new Entity_Mapping());
        }
        public DbSet<Entity> Data { get; set; }
    }
    public abstract class Entity
    {
        public int ID { get; set; }
        public string ShortName { get; set; }
    }
    public class FirstEntity: Entity
    {
        public int Value { get; set; }
    }
    public class SecondEntity: Entity
    {
        public int Height { get; set; }
    }
    public class Entity_Mapping : EntityTypeConfiguration<Entity>
    {
        public Entity_Mapping()
        {
            this.HasKey(t => t.ID);
            this.ToTable("MyEntity");
            this.Property(t => t.ID).HasColumnName("ID").HasDatabaseGeneratedOption(new Nullable<DatabaseGeneratedOption>(DatabaseGeneratedOption.None));
            this.Map<FirstEntity>(m =>
            {
                m.Property(t => t.Value).HasColumnName("Int1");
                m.Requires("TypeID").HasValue(1);
            });
            this.Map<SecondEntity>(m =>
            {
                m.Property(t => t.Height).HasColumnName("Int1");
                m.Requires("TypeID").HasValue(2);
            });
            this.Property(t => t.ShortName).HasColumnName("ShortName");
        }
    }
}
Closed Oct 31, 2013 at 8:36 PM by lukew
Verified merged into 6.0.2 branch

comments

ajcvickers wrote Oct 1, 2013 at 4:37 PM

Note for triage: this repros on EF6-rc1 and latest nightly. The culprit appears to be the Property call inside the Map call. Allowing this call was introduced in EF6.
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingConfiguration.<>c__DisplayClass1b.<Config
urePropertyMappings>b__19(Tuple`2 pm)
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
   at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(
IEnumerable`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConf
iguration)
   at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingConfiguration.ConfigurePropertyMappings(I
List`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.<>c__DisplayClass4d.<ConfigurePr
opertyMappings>b__47(EntityMappingConfiguration c)
   at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigurePropertyMappings(DbData
baseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest, Boolean allowOverride)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType,
 DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping data
baseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping
, DbProviderManifest providerManifest)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at ConsoleApplication27.Program.Main(String[] args) in c:\Stuff\ConsoleApplication27\ConsoleApplication27\Program.cs:
line 18

GraySerg wrote Oct 2, 2013 at 5:32 AM

I can fix it by adding block m.ToTable("MyEntity"); into each section.
public class Entity_Mapping : EntityTypeConfiguration<Entity>
{
    public Entity_Mapping()
    {
        this.HasKey(t => t.ID);
        this.ToTable("MyEntity");
        this.Property(t => t.ID).HasColumnName("ID").HasDatabaseGeneratedOption(new Nullable<DatabaseGeneratedOption>(DatabaseGeneratedOption.None));
        this.Map<FirstEntity>(m =>
        {
            m.ToTable("MyEntity");
            m.Property(t => t.Value).HasColumnName("Int1");
            m.Requires("TypeID").HasValue(1);
        });
        this.Map<SecondEntity>(m =>
        {
            m.ToTable("MyEntity");
            m.Property(t => t.Height).HasColumnName("Int1");
            m.Requires("TypeID").HasValue(2);
        });
        this.Property(t => t.ShortName).HasColumnName("ShortName");
    }
}

emilcicos wrote Oct 17, 2013 at 6:37 PM

commit 461036be1dc907f1a7bd17fdb8004ebc35e873d3

lukew wrote Oct 24, 2013 at 10:01 PM

Confirmed, added logic to populate the table name when not explicitly mapped and updated and added logic to handle null cases

** Closed by lukew 10/24/2013 3:01PM

ajcvickers wrote Oct 30, 2013 at 9:40 PM

This has been merged into the 6.0.2 branch and is now ready to verify in that branch.