This project has moved and is read-only. For the latest updates, please go here.

ParentNotMappedYetException on TBT BulkInsert

Aug 8, 2014 at 2:41 AM
Edited Aug 8, 2014 at 2:58 AM
Hi,

I'm trying to bulk load a master / detail relationship where the master row has an identity PK and the children have a FK relationship to the parent.
    public partial class MeterDataSet {
        public MeterDataSet() {
            this.IntervalData = new HashSet<IntervalData>();
        }

        [Key]
        public int Id { get; set; }
        public string UOM { get; set; }
        public byte IntervalLength { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public virtual ICollection<IntervalData > IntervalData { get; set; }
    }

    public partial class SiteMeterDataSet : MeterDataSet {
        public string Site{ get; set; }
    }

    public partial class IntervalData {
        public IntervalData() { }

        [Key]
        public int Id { get; set; }
        public DateTime IntervalDate { get; set; }
        public byte Interval { get; set; }
        public decimal IntervalValue { get; set; }
        public string QualityFlag { get; set; }
        public string QualityMethod { get; set; }
        public virtual MeterDataSet MeterDataSet { get; set; }
        public int MeterDataSetId { get; set; }
    }
My approach has been
    // Detach IntervalData
    var details = master.IntervalData;
    master.IntervalData = null;

    // Save MeterDataSet row normally 
    model.MeterDataSets.Add(master);
    model.SaveChanges();

    // Get Id from saved MeterDataSet row
     foreach (var detail in details) {
        detail.MeterDataSetId = master.Id;
        }
    // Bulk insert IntervalData for performance
    model.BulkInsert(details);

    // Reattach IntervalData
    master.IntervalData = details;
This was working until I introduced the Table-Per-Type (TBT) hierarchy on MeterDataSet / SiteMeterDataSet.

Now when I hit the model.BulkInsert() statement ParentNotMappedYetException is thrown from MapperBase.cs:line 334 (EF 6.1)

Note, the exception is only thrown when I use BulkInsert - if I use the standard SaveChanges() it works fine.

Any ideas how I should approach this problem?

Regards
Dave
Aug 8, 2014 at 6:19 AM
Edited Aug 8, 2014 at 6:21 AM
Actually it looks like this is an Exception which is thrown and caught within the EntityFramework.MappingAPI but at some stage I've had break on all Exceptions set in Visual Studio and somehow clicked on Always Break with this exception thrown. So the code above is working - oops ;)
Marked as answer by dwaterworth on 8/7/2014 at 10:21 PM