I have two entities one for a class another for a student and I have a list of registered students in the class entity. The problem I'm having is that after I add one student to the list of RegisteredStudents and then log off and log back in as another student and try to register another student, the RegisteredStudents list is empty! It doesn't remember the previously added student.
FYI - this is EF 6+, ASP.Net MVC
Here is my code.
Parent entity with list of children
public class Event
{
public Event() {
RegisteredStudents = new List<Student> ();
}
public int Name{ get; set; }
public DateTime Time{ get; set; }
// other properties
public List<Student> RegisteredStudents { get; set; }
}
Child entity that is being added to the list in the parent
public class Student
{
public Student() {}
public int Name{ get; set; }
public DateTime Time{ get; set; }
// other properties
}
Here is how I'm adding a new student entity. I get an event and student
then I add the student. This is done in a service class method and so if I have logged in as a new student it will be the same event but a different student. Either way the list is empty after added the first student and saving the context.
var student= dbContext.Profiles.Where(i => i.ApplicationUserGuid == userId).First();
var event = dbContext.SpaceEvents.Where(j => j.SpaceEventId == eventId).First();
// do some other studff here then add the student
event.RegisteredStudents.Add(student);
dbContext.SaveChanges();
when I hover over RegisteredStudents after adding a single student and trying now to add a second, it's empty!
As your title suggests its one-to-many relationship so I assume your student class looks like the following
public class Student
{
public Student() {}
public int Name{ get; set; }
public DateTime Time{ get; set; }
// other properties
public string EventId {get;set;}
public virtual Event Event {get; set; }
}
First make sure your entities are loaded correctly coming directly from db context. Some information on loading entities
Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook. MSDN Reference
You can try the following things to properly add students to an event
1) Update event in Student's object
student.Event = event; //add this line here
event.RegisteredStudents.Add(student);
dbContext.SaveChanges();
2) Update foreign key in student's table
student.EventId = event.Id; //or add this line
event.RegisteredStudents.Add(student);
dbContext.SaveChanges();
3) Check the state of Event and update it accordingly
var state = dbContext.Entry(event).State;
if(state == EntityState.Detached)
dbContext.Events.Attach(event);
dbContext.Entry(event).State = EntityState.Modified;
I will suggest you to read more about loading related entities here if you still face any issue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.