I have a routine that returns mostly correct data. I pass it a string Id code, it creates a model, gets data from a database, fills in the model, and then passes the model back. The return model looks correct and has correct data except the List’s in the model do not contain any data.
The model looks similar to this one
public class SubjectDB
{
public string SubjectId { get; set; }
.
.
List<AddressDB> Address { get; set; }
.
.
[Key]
public int Id { get; set; }
}
public class DBEntities: DbContent
{
public DbSet<SubjectDB> SubjectDB { get; set; }
public DbSet<AddressDB> AddressDB { get; set; }
.
.
}
The subroutine is called using this code
SubjectDB xsubject = new SubjectDB();
xsubject = (SubjectDB)GetNewSubject(model.Subject.Id);
The subroutine is
Public object GetNewSubject( string Id )
{
SubjectDB subjectdb = new SubjectDB();
DBEntites db = new DBEntities();
var subjectxx = db.SubjectDB.First(x => x.SubjectId == Id);
var addressxx = db.AddressDB.First(x => x.SubjectId == Id);
.
.
subjectdb.SubjectId = subjectxx.Subjectid;
.
.
List<AddressDB> Address = new List<AddressDB>();
Address.Add(addressxx);
.
.
return(subjectdb);
}
I can mouse over the subjectdb in the subroutine and see all the data including the data in the List’s, but when I mouse over the returned object “xsubject”, I see all the correct returned data but the List’s are empty.
Found the missing piece of the puzzel. I was not initializing the Lists even if they are public.
Model modified to:
Now it all works. the DbSet commands get the data from the database and the LIST get updated correctly and when the model is passed back to the main, all the data is there.
Thanks to all that helped me see where I had gone wrong.