简体   繁体   English

从数据库中的对象检索数组或列表

[英]Retrieving Array or list from object in database

I'm having trouble when I pull an object from the database with getting the framework to also get an an array in the object as well. 当我从数据库中拉出对象并同时获得框架以及对象中的数组时,我遇到了麻烦。 I found that for sub objects the .Include("subobject") seems to work, but I can't get it to work for arrays or lists. 我发现对于.sub对象,.include(“ subobject”)似乎可以工作,但是我无法使其用于数组或列表。

My Model: 我的模特:

public class RunData
{
   [Key]
   [Required]
   public int id { get; set; }
   public List<RunElement> Runs { get; set; }
   public string[] DataLabels { get; set; }
}

List of Entities: 实体清单:

public class ProgramEntities:DbContext
{
    public DbSet<RunData> RunData { get; set; }
    public DbSet<RunElement> RunElement { get; set; }
}

Controller Code: 控制器代码:

    public ViewResult Details(int id)
    {
        RunData rundata = (from RunData in db.RunData.Include("Runs").in where RunData.id == id select RunData).First();
        return View(rundata);
    }

I did have all kinds of trouble with it not returning the list of Runs objects, but when I did the .Include("Runs") that fixed the problem. 我确实遇到了种种麻烦,因为它没有返回Runs对象列表,但是当我执行.include(“ Runs”)时,就解决了该问题。 So, now my trouble is the DataLabels string array. 因此,现在的麻烦是DataLabels字符串数组。 If I try .Include("DataLabels") the program fails and says: 如果我尝试.include(“ DataLabels”),程序将失败并显示:

A specified Include path is not valid. 指定的包含路径无效。 The EntityType 'Program_Dataviewer.Models.RunData' does not declare a navigation property with the name 'DataLabels'. EntityType“ Program_Dataviewer.Models.RunData”未声明名称为“ DataLabels”的导航属性。

I have searched online some, I'm not seeing any clear cut answers. 我在网上搜索了一些内容,但没有找到明确的答案。 Thank you for the help. 感谢您的帮助。

You can not have collections of primitives in your data model, since each collection must be mapped to a table in the relational space (think about it - how is the database going to organize/save your collection?). 您的数据模型中不能包含原语集合,因为每个集合都必须映射到关系空间中的表(考虑一下-数据库将如何组织/保存您的集合?)。 What you can do is introduce a table / entity for DataLabels , eg something like this: 您可以做的是为DataLabels引入一个表/实体,例如:

public class RunData
{
   [Key]
   [Required]
   public int id { get; set; }
   public List<RunElement> Runs { get; set; }
   public List<DataLabel> DataLabels { get; set; }
}

public class DataLabel
{
  [Key]
  public int id { get; set; }
  public string LabelName { get; set; }
}

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

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