简体   繁体   English

使用实体框架的 CSV 到级联模型

[英]CSV to Cascading Model using Entity Framework

I need to turn an automatic CSV file into multiple database columns using Entity Framework.我需要使用实体框架将自动 CSV 文件转换为多个数据库列。 It is set up so that each model has children.它的设置使得每个模型都有孩子。 So that Animal contains a list of Types which contain a list of Classification .所以Animal包含一个Types列表,其中包含一个Classification列表。 In this way Classification is a grandchild of Animal这样, ClassificationAnimal的孙子

Right now I have these three models that need to be filled by the CSV file.现在我有这三个模型需要由 CSV 文件填充。 The file is formatted in the following way:该文件的格式如下:

测试数据

They are then pulled from the API into a Windows Desktop App as a cascading dropdown box.然后将它们从 API 中拉入 Windows 桌面应用程序作为级联下拉框。 So far I've tried adding them to separate lists however that did not upload when using Entity Framework.到目前为止,我已经尝试将它们添加到单独的列表中,但是在使用实体框架时没有上传。 The current way is to try to cascade down the list however I get an error当前的方法是尝试级联列表但是我收到错误

Sequence contains no events序列不包含事件

Here is the portion of the code that I am having a problem with (had to edit due to work rules so classes are different):这是我遇到问题的代码部分(由于工作规则而不得不进行编辑,因此类不同):

var Animal = new List<AnimalModel>();

var lines = await ReadStreamAsync(new StreamReader(uploadModel.File.OpenReadStream()));

foreach(string l in lines)
{
   Animal.Add(new AnimalModel
   {
      AnimalName = cells[0],
   });

   Animal.Last().Type.Add(new TypeModel
      {
         TypeName = cells[1],
      });

   Animal.Last().Type.Last().Classification.Add(new ClassificationModel
   {
      Type = Type.Last(),
      ClassificationName = cells[2],
      Color = cells[3],
      Age = cells[4]
   });
}

I resolved the issue.我解决了这个问题。 I needed to initialize the list within the code as I am not doing so within the model.我需要在代码中初始化列表,因为我没有在模型中这样做。 The following worked:以下工作:

var Animal = new List<AnimalModel>();

var lines = await ReadStreamAsync(new StreamReader(uploadModel.File.OpenReadStream()));

foreach(string l in lines)
{
   Animal.Add(new AnimalModel
   {
      AnimalName = cells[0],
      Type = new List<TypeModel>()
      {
          new TypeModel()
          {
              TypeName = cells[1]
          }
      }
   });

And so on for the grandchild.对于孙子,依此类推。 I will have to clean this up as it is quite messy but this works for now.我将不得不清理它,因为它很乱,但现在有效。

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

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