0

Let's say I have a really simple class:

public class myClass{
    public int Id {get;set;}
    public object SomeProperty {get;set;}
    public List<string> ListOfStrings {get;set;}
}

whenever I try to retrieve myClass entity from the database, ListOfStrings will be empty even though I have accurately debugged into the CRUD process to make sure that ListOfStrings is well populated before Insert() or Update() method is called on the entity

garry man
  • 445
  • 4
  • 14

1 Answers1

1

The entity you're saving in the collection should have an ID property and you should link the two entities using foreign keys. If you're using entity framework, you could define your classes like this:

public class SomeEntity
{
    public int SomeEntityID {get;set;}
    public string Message {get;set;}
    public MyClassID {get;set;}
}

public class MyClass
{
    public int MyClassID {get;set;}
    public object SomeProperty {get;set;}
    public List<SomeEntity> ListOfSomeEntity {get;set;}
} 

Then you can retrieve your entity like this:

var myClass = context.MyClass.Include(c=>c.ListOfSomeEntity)
              .Single(c=>c.MyClassID == 1);
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61