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.
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. So, now my trouble is the DataLabels string array. If I try .Include(“DataLabels”) the program fails and says:
A specified Include path is not valid. The EntityType
‘Program_Dataviewer.Models.RunData’ does not declare a navigation
property with the name ‘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, e.g. something like this: