MikaelEliasson / EntityFramework.Utilities

Provides extensions for EntityFramework that doesn't exist out of the box like delete and update by query and bulk inserts
443 stars 175 forks source link

InsertAll: "Object reference not set to an instance of an object." #63

Open john-warpdevelopment opened 8 years ago

john-warpdevelopment commented 8 years ago

I'm using EF 6 Code First, and when I try and inserting +-1000 I'm getting the error "Object reference not set to an instance of an object.". Wonder if the issue is with handeling optional collumns.

e.g. the following model

class Person{
    int PersonId;
    int AddressId;
    Address Address;
...
}

Here is a stack trace:

at EntityFramework.Utilities.EFDataReader1.<.ctor>b__1(ColumnMapping p) at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at EntityFramework.Utilities.EFDataReader1..ctor(IEnumerable1 items, IEnumerable1 properties) at EntityFramework.Utilities.SqlQueryProvider.InsertItems[T](IEnumerable1 items, String schema, String tableName, IList1 properties, DbConnection storeConnection, Nullable1 batchSize) at EntityFramework.Utilities.EFBatchOperation2.InsertAll[TEntity](IEnumerable1 items, DbConnection connection, Nullable1 batchSize)

MikaelEliasson commented 8 years ago

Hmm, might it be that the Address is null? Complex types most likely has to be initiated (like when you save with EF the normal way)

john-warpdevelopment commented 8 years ago

I did try instantiating the complex types, didn't help here is the complete model:

  public partial class Person : BaseEntity
    {
        #region Constructor
        public Person()
        {
            this.CreatedOnUtc = DateTime.Now;
            this.ModifiedOnUtc = DateTime.Now;
            this.Gender = Common.Gender.Male;
            this.PersonGuid = Guid.NewGuid();
            this.PersonStatus = PersonStatus.Enabled;
            this.Memberships = new HashSet<Member>();
            this.Leads = new HashSet<Lead>();
            this.LeadComments = new HashSet<LeadComment>();
            this.LeadAssigns = new HashSet<LeadAssign>();
            this.LeaveRequests = new HashSet<Leave>();
            this.LeaveApprovals = new HashSet<Leave>();
            this.Assets = new HashSet<Asset>();
            this.ContactDetails = new HashSet<ContactDetail>();
            this.Accounts = new HashSet<Account>();
            this.Documents = new HashSet<Document>();
        }
        #endregion

        #region Value Properties
        public Int32 PersonId { get; set; }
        public Nullable<int> AddressId { get; set; }

        public DateTime CreatedOnUtc { get; set; }
        public DateTime ModifiedOnUtc { get; set; }
        public Guid PersonGuid { get; set; }
        public String Title { get; set; }
        public String FirstName { get; set; }
        public String MiddleNames { get; set; }
        public String Surname { get; set; }
        public String Alias { get; set; }
        public Nullable<System.DateTime> Birthday { get; set; }

        public String Email { get; set; }
        internal string GenderEnum { get; set; }
        internal string PersonStatusEnum { get; set; }
        public String PasswordSalt { get; set; }
        public String Password { get; set; }
        public String Tag { get; set; }
        public string IdNumber { get; set; }
        public string AccountNumber { get; set; }

        #endregion

        #region Enumeration Properties
        public Gender Gender
        {
            get { return (Gender)Enum.Parse(typeof(Gender), this.GenderEnum); }
            set { this.GenderEnum = value.ToString(); }
        }

        public PersonStatus PersonStatus
        {
            get { return (PersonStatus)Enum.Parse(typeof(PersonStatus), this.PersonStatusEnum); }
            set { this.PersonStatusEnum = value.ToString(); }
        }
        #endregion

        #region Navigation Properties

        public ICollection<Member> Memberships { get; set; }

        public ICollection<Lead> Leads { get; set; }

        public ICollection<LeadComment> LeadComments { get; set; }

        public ICollection<LeadAssign> LeadAssigns { get; set; }

        public Address Address { get; set; }

        public ICollection<Leave> LeaveApprovals { get; set; }

        public ICollection<Leave> LeaveRequests { get; set; }

        public ICollection<Asset> Assets { get; set; }

        public PersonLeaveInfo PersonLeaveInfo { get; set; }

        public ICollection<ContactDetail> ContactDetails { get; set; }

        public ICollection<Account> Accounts { get; set; }

        public ICollection<Document> Documents { get; set; }
        #endregion
    }

what's weird is it worked for some of my other entities that have complex types , and enumerations same as this one.

MikaelEliasson commented 8 years ago

I might need to debug this one. Did the types it work for have the same base class?

If they did I suspect the internal properties but I'll have to try it.

john-warpdevelopment commented 8 years ago

Yes , that base class is empty it's just used for generics.

catinodeh commented 8 years ago

I had the same problem. My object has only primitive properties (date, string and int) and my table accepts NULL on all of them. Any clue what could be causing this ?

This is my exception:

2016-06-03 12:13:12,633 [8] ERROR [Controllers.LogsController+d1.MoveNext] System.NullReferenceException: Object reference not set to an instance of an object. at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at EntityFramework.Utilities.EfMappingFactory.GetMappingsForContext(DbContext context) at EntityFramework.Utilities.EFBatchOperation2.InsertAll[TEntity](IEnumerable1 items, DbConnection connection, Nullable1 batchSize) at WRE.George.API.Controllers.LogsController.d1.MoveNext() in D:\TFS Projects\WRE.George\WRE.George.API\Controllers\LogsController.cs:line 59