简体   繁体   English

将列表项从嵌套列表添加到实体框架上下文

[英]Adding List Items to an Entity Framework Context from a Nested List

I am trying to insert some records into a database using Entity Framework. 我正在尝试使用实体框架将一些记录插入数据库。 What I am struggling with is accessing the properties of the internal lists. 我正在努力的是访问内部列表的属性。

The inner lists have the following structure: 内部列表具有以下结构:

 public class Innerlist { public string item1 { get; set; } public List<string> item2 { get; set; } public List<object> item3 { get; set; } public int item4 {get; set;} etc... } 

The above list is then wrapped in another list. 然后将上面的列表包装在另一个列表中。 My question is how do I access the properties Item1, Item2, and Item3? 我的问题是如何访问属性Item1,Item2和Item3?

My looping code has the following structure: 我的循环代码具有以下结构:

using(var context = new fooContext())
{
    foreach (var innerList in Outerlist)
    {         
        try
        {
            context.fooEntity.Add(innerList);
        }
        catch (Exception ex)
        {
            Console.WriteLine("\n\n" + ex);
            Console.ReadLine();
        }
    }
}

I tried using a foreach loop that looped through the outer list and then another to loop through the inner list. 我试过使用一个foreach循环,该循环遍历外部列表,然后再循环遍历内部列表。 This however generated an error about the class not containing a public definition of 'GetEnumerator'. 但是,这会产生有关该类的错误,该类不包含“ GetEnumerator”的公共定义。

The inner foreach loop is structured like this: 内部的foreach循环的结构如下:

   foreach (var innerList in outerList)
{
    foreach(var item in innerList)
    {
        try
        {
            context.fooEntity.Add(item);
        }
        catch (Exception ex)
        {
            Console.WriteLine("\n\n" + ex);
            Console.ReadLine();
        }
    }

}

I am now getting the errors: 'The best overloaded match has some invalid arguments.' 我现在收到错误消息:“最佳重载匹配具有一些无效的参数。” And 'Cannot convert from object to InnerList Class.' 和“无法从对象转换为InnerList类。”

EDIT 编辑

I should add that this is being converted into a list using JSON.net which is taking a JSON string and converting it into a list via the innerList class, which exists as Entity Framework Model class. 我应该补充一点,这是使用JSON.net将其转换为列表的,JSON.net接收JSON字符串,然后通过innerList类将其转换为列表,该类作为Entity Framework Model类存在。

Any help would be great. 任何帮助都会很棒。 Cheers. 干杯。

Your Innerlist class is not a list. 您的内部列表类别不是列表。 It is a class that contain a list. 这是一个包含列表的类。 Make your Innerlist object implement IEnumerable 使您的内部列表对象实现IEnumerable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM