1

I'm having an issue with a site that I'm writing in C# ASP using Entity Framework for the database. One of the data models that I'm using to store and retrieve data called DowntimeEvent contains 2 Lists AffectedSystems and AffectedDepartments. While I'm running the application in Visual Studio those lists store and retrieve just fine. But if I stop and restart the application the DowntimeEvents are still stored in my database, however the Lists for Affected Departments, and Affected Systems are null when I try to retrieve them.

Here's the Model I'm using to store the data

public class DowntimeEventModel
{
    [Key]
    public int ID { get; set; }
    [Required]
    public DateTime StartDateTime { get; set; }
    [Required]
    public DateTime EndDateTime { get; set; }
    public int LocationID { get; set; }
    [Required]
    public string Description { get; set; }
    //public int DepartmentID { get; set; }
    //public int SystemID { get; set; }
    public virtual ICollection<int> AffectedSystems { get; set; }
    public virtual ICollection<int> AffectedDepartments { get; set; }
    //public virtual ICollection<SystemModel> AffectedSystems { get; set; }
    //public virtual ICollection<DepartmentModel> AffectedDepartments { get; set; }
}

Here's an example Controller of how I'm saving the data, and by the way this seems to be working just fine in storing the lists.

[HttpPost]
    public ActionResult DowntimeEvent(DowntimeEventModel downtimeEvent)
    {
        PowerteqContext.DowntimeEvents.Add(downtimeEvent);
        PowerteqContext.SaveChanges();
        return View(SetupDowntimeEventViewModel());
    }

It was this method that tipped me off to there being an issue with data retrieval after trying to write this report and trying to figure out why AffectedSystems was sometimes null and sometimes not. In the inner foreach loop I tried to access the ListAffectedSystems directly just to see if the loop might not be null that way and it is after a restart, but it's not if I add them and don't restart.

        public ActionResult ReportUptimeBySystems()
    {

        var EndTime = DateTime.Now;
        var StartTime = DateTime.Now.AddDays(-28);
        var uptimeHours = new TimeSpan(1);
        if (EndTime != StartTime)
            uptimeHours = EndTime - StartTime;

        List<ReportUptimeBySystem> SysUps = new List<ReportUptimeBySystem>();
        var DownTimes = PowerteqContext.DowntimeEvents.AsEnumerable();
        var Systems = PowerteqContext.Systems.AsEnumerable();

        foreach (var x in Systems)
        {
            ReportUptimeBySystem sys = new ReportUptimeBySystem();
            sys.SystemTimeUP = uptimeHours;
            sys.SystemName = x.SystemName;
            foreach (var y in DownTimes)
            {
                if(PowerteqContext.DowntimeEvents.Find(y.ID).AffectedSystems.Contains(x.ID))
                {
                    sys.SystemTimeUP -= y.StartDateTime - y.EndDateTime;
                }
            }
            SysUps.Add(sys);
        }
        return View(SysUps);
    }

Another developer suggested that the issue may be in my Entity Framework Configuration. But I don't know where to look to even try to fix that.

For reference the whole application can be found here. The database I'm using is Microsoft SQL Serverhere

Jazzeroki
  • 143
  • 1
  • 14

2 Answers2

1

Entity framework will only automatically load relationships if it finds properties representing collections of another entity. It also must be able to identify foreign keys. By standards SystemModel and DepartmentModel should have a property DowntimeEventID, otherwise you'll have to inform it how to do this for you.

You should also ensure that lazy loading isn't disabled.

https://msdn.microsoft.com/en-us/data/jj574232.aspx

Disable lazy loading by default in Entity Framework 4

Here is a good example from a related question. Many-to-many mapping table

public class DowntimeEventModel
{
    [Key]
    public int ID { get; set; }
    [Required]
    public DateTime StartDateTime { get; set; }
    [Required]
    public DateTime EndDateTime { get; set; }
    public int LocationID { get; set; }
    [Required]
    public string Description { get; set; }
    public virtual ICollection<SystemModel> AffectedSystems { get; set; }
    public virtual ICollection<DepartmentModel> AffectedDepartments { get; set; }
}
Community
  • 1
  • 1
jtimperley
  • 2,394
  • 10
  • 10
0

Assuming AffectedSystems and AffectedDepartments are also EF entities with which DowntimeEventModel is linked with foreign keys with, you could try to explictly included them when you fetch your DowntimeEventModel results as such:

PowerteqContext.DowntimeEventModel.Include("DowntimeEventModel.AffectedSystems").Include("DowntimeEventModel.AffectedDepartments").ToList();
Francis Ducharme
  • 4,277
  • 5
  • 32
  • 66