模型包含使用entityFramework的IEnumerable属性插入

时间:2015-05-13 15:45:48

标签: c# entity-framework

我有以下两种型号:

class Parent {
    static yell() {
        console.log('yell')
    }
}

class Child extends Parent {

}

Child.yell();

一旦"警报"对象属性中包含值,我试图使用EntityFramework将此对象嵌入到我的SQL DB中,如下所示:

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; }
}

AlertObjectFactory.cs和负责构建AlertRecords列表的类在这里(它们是大类文件) https://gist.github.com/anonymous/67a2ae0192257ac51f39

&#34;警报&#34;表中正在填充数据,但是有4条记录    IEnumerable Records 没有被插入...

EF可以实现此功能吗?

1 个答案:

答案 0 :(得分:4)

尝试将IEnumerable更改为实施ICollection的内容,例如List 有关详细信息,请参阅this答案