使用EntityFramework包含IEnumerable属性插入的模型 [英] Model Containing a IEnumerable property insert using entityFramework

查看:54
本文介绍了使用EntityFramework包含IEnumerable属性插入的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两种模型:

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

一旦 Alert对象属性中包含值,我将尝试使用EntityFramework将此对象插入到我的SQL DB中,如下所示:

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

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

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

警报表中已填充数据,但是
IEnumerable Records
中的4条记录不会被插入...

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

EF是否可以使用此功能?

Is this functionality possible with EF?

推荐答案

尝试将您的 IEnumerable 更改为实现 ICollection 的内容,例如 List

参见答案更多详情

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

这篇关于使用EntityFramework包含IEnumerable属性插入的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆