This is a migrated thread and some comments may be shown as answers.

NullReferenceException on building join table association

0 Answers 39 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Neal Chamen
Top achievements
Rank 1
Neal Chamen asked on 27 Oct 2016, 11:00 AM
As the title states I am getting a null reference exception on build of my DAL. 

I have the models 
public Workflow()
        {
            this.Regions = new List<Region>();
            this.StoreActions = new List<StoreAction>();
            this.InvestigationTriggers = new List<InvestigationTrigger>();
            this.IncidentInvolving = new List<IncidentInvolving>();
            this.IncidentTriggers = new List<IncidentTrigger>();
 
            this._regions = new List<Region>();
            this._storeActions = new List<StoreAction>();
            this._investigationTriggers = new List<InvestigationTrigger>();
            this._incidentInvolving = new List<IncidentInvolving>();
            this._incidentTriggers = new List<IncidentTrigger>();
 
        }
 
        private Guid _id;
        [Key()]
        [Display(Name = "Workflow Id")]
        public virtual Guid ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
 
        private string _name;
        [StringLength(200)]
        [Required()]
        [Display(Name = "Name")]
        public virtual string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
 
        private string _description;
        [StringLength(500)]
        [Display(Name = "Description")]
        public virtual string Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
            }
        }
 
        private DateTime _startDate;
        [Display(Name = "Start Date")]
        public virtual DateTime StartDate
        {
            get
            {
                return _startDate;
            }
            set
            {
                _startDate = value;
            }
        }
 
        private DateTime _endDate;
        [Display(Name = "End Date")]
        public virtual DateTime EndDate
        {
            get
            {
                return _endDate;
            }
            set
            {
                _endDate = value;
            }
        }
 
        private DateTime _createdOn;
        [Display(Name = "Created On")]
        public virtual DateTime CreatedOn
        {
            get
            {
                return _createdOn;
            }
            set
            {
                _createdOn = value;
            }
        }
 
        private DateTime _lastUpdatedOn;
        [Display(Name = "Last Updated On")]
        public virtual DateTime LastUpdatedOn
        {
            get
            {
                return _lastUpdatedOn;
            }
            set
            {
                _lastUpdatedOn = value;
            }
        }
 
        private bool _isDeleted;
        [Display(Name = "Is Deleted")]
        public virtual bool IsDeleted
        {
            get
            {
                return _isDeleted;
            }
            set
            {
                _isDeleted = value;
            }
        }
 
        private Guid _createdBy;
        [Required()]
        [Display(Name = "Created By")]
        public virtual Guid CreatedBy
        {
            get
            {
                return _createdBy;
            }
            set
            {
                _createdBy = value;
            }
        }
 
        private IList<Region> _regions;
        [Display(Name = "Regions")]
        public virtual IList<Region> Regions
        {
            get
            {
                return _regions;
            }
            set
            {
                _regions = value;
            }
        }
 
        private IList<StoreAction> _storeActions;
        [Display(Name = "Store Actions")]
        public virtual IList<StoreAction> StoreActions
        {
            get
            {
                return _storeActions;
            }
            set
            {
                _storeActions = value;
            }
        }
        private IList<InvestigationTrigger> _investigationTriggers;
        [Display(Name = "Investigation Triggers")]
        public virtual IList<InvestigationTrigger> InvestigationTriggers
        {
            get
            {
                return _investigationTriggers;
            }
            set
            {
                _investigationTriggers = value;
            }
        }
 
        private IList<IncidentTrigger
public partial class Region
    {
        public Region()
        {
            this.Workflows = new List<Workflow>();
            this._workflows = new List<Workflow>();
        }
 
        private Guid _id;
        [Required()]
        [Key()]
        [Display(Name = "Id")]
        public virtual Guid ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
 
        private string _name;
        [StringLength(200)]
        [Required()]
        [Display(Name = "Name")]
        public virtual string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
 
        private IList<Workflow> _workflows;
        [Display(Name = "Workflows")]
        public virtual IList<Workflow> Workflows
        {
            get
            {
                return _workflows;
            }
            set
            {
                _workflows = value;
            }
        }
    }
> _incidentTriggers;
        [Display(Name = "Incident Triggers")]
        public virtual IList<IncidentTrigger> IncidentTriggers
        {
            get
            {
                return _incidentTriggers;
            }
            set
            {
                _incidentTriggers = value;
            }
        }
 
        private IList<IncidentInvolving> _incidentInvolving;
        [Display(Name = "Incident Including")]
        public virtual IList<IncidentInvolving> IncidentInvolving
        {
            get
            {
                return _incidentInvolving;
            }
            set
            {
                _incidentInvolving = value;
            }
        }

 

and use the association

configuration.HasAssociation(w => w.Regions)
                .WithOpposite(r => r.Workflows)
                .MapJoinTable("WorkflowRegions", (w, r) => new
                {
                    WorkflowId = w.ID,
                    RegionId = r.ID
                });

 

but the exception is thrown on the last block of code provided. 

If I comment it out, the project builds fine and I am able to use the entities to insert into the database so I am fairly sure that it is just the Association which is failing. 

If I need to provide more information then I can do. 

Thanks in advance. 

Tags
General Discussions
Asked by
Neal Chamen
Top achievements
Rank 1
Share this question
or