1

I have the following 2 models:

public class Alert
{
    public int Id { get; set; }
    public DateTime AlertDatetime { get; set; }
    public bool Unread { get; set; }
    public int? ReadByUserId { get; set; }
    public DateTime? ReadDateTime { get; set; }
    public DateTime ImportDateTime { get; set; }
    public bool AlertHasRecords { get; set; }

    //Error Reporting and Recording.
    public bool Error { get; set; }
    public string ErrorText { get; set; }

    public virtual IEnumerable<AlertRecord> Records { get; set; }
}


public class AlertRecord
{
    public int Id { get; set; }
    public string HospitalNumber { get; set; }
    public string ForeName { get; set; }
    public string SurName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public DateTime EventDateTime { get; set; }
    public string CrisNumber { get; set; }

    public DateTime CreationDateTime { get; set; }

    //Link it back to the master Alert!
    public int AlertId { get; set; }
    public virtual Alert Alert { get; set; }
}

Once the "Alert" Object properties have values in them, I am trying to use EntityFramework to inset this object into my SQL DB like this:

class Program
{
    private static Alert MainAlert = new Alert();
    private static PrimaryDBContext db = new PrimaryDBContext();

    static void Main(string[] args)
    {
        MainAlert = AlertObjectFactory.GetAlertObject();            
        db.Alerts.Add(MainAlert);
        db.SaveChanges();
    }
}

The AlertObjectFactory.cs and The Class responsible for building the list of AlertRecords are here(They are large class files) https://gist.github.com/anonymous/67a2ae0192257ac51f39

The "Alert" Table is being populated with data, however the 4 records in the IEnumerable Records are not being inserted...

Is this functionality possible with EF?

tornup
  • 263
  • 1
  • 4
  • 15
  • 3
    I don't see where you're adding 4 `AlertRecords` to your `MainAlert`. – D Stanley May 13 '15 at 15:48
  • 1
    We need to see your code in the `GetAlertObject()` method. We still don't see where you're populating the data. – Drew Kennedy May 13 '15 at 15:53
  • @DStanley I have edited my question to show the method call to my "AlertObjectFactory" the Class responsible for filling the object with its data. – tornup May 13 '15 at 15:53
  • I have uploaded the 2 class files in question to Gist as they are quite large. The class generating the data within the Records property is: AlertRecordImporter.GetAlertObjects() – tornup May 13 '15 at 16:00

1 Answers1

6

Try changing your IEnumerable to something that implements ICollection such as List
See this answer for more details

Community
  • 1
  • 1
user2697817
  • 1,438
  • 1
  • 15
  • 27