简体   繁体   English

不记得之前为一对多关系添加的实体的实体列表

[英]List of entities not remembering previously added entities for a one-to-many relationship

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!我遇到的问题是,在我将一名学生添加到 RegisteredStudents 列表然后注销并以另一名学生的身份重新登录并尝试注册另一名学生后,RegisteredStudents 列表是空的! It doesn't remember the previously added student.它不记得之前添加的学生。

FYI - this is EF 6+, ASP.Net MVC仅供参考 - 这是 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 // 在这里做一些其他的stuff然后添加学生

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!添加单个学生并尝试添加第二个后,当我将鼠标悬停在 RegisteredStudents 上时,它是空的!

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.使用 POCO 实体类型时,通过创建派生代理类型的实例,然后覆盖虚拟属性以添加加载钩子来实现延迟加载。 MSDN Reference MSDN 参考

You can try the following things to properly add students to an event您可以尝试以下方法来正确地将学生添加到活动中

1) Update event in Student's object 1) 学生对象中的更新事件

student.Event = event; //add this line here 
event.RegisteredStudents.Add(student);
dbContext.SaveChanges();

2) Update foreign key in student's table 2) 更新学生表中的外键

student.EventId = event.Id; //or add this line
event.RegisteredStudents.Add(student);
dbContext.SaveChanges();

3) Check the state of Event and update it accordingly 3)检查事件的状态并相应地更新它

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如果您仍然遇到任何问题,我建议您在此处阅读有关加载相关实体的更多信息

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

相关问题 使用一对多关系实体列表的属性更新db上的实体 - Update entity on db with a property of list of entities with one-to-many relationship 删除一对多关系中的相关实体 - deleting related entities in a one-to-many relationship EF 7:如何以一对多关系加载相关实体 - EF 7: How to load related entities in a One-to-many relationship 如何配置版本化实体之间的一对多/多对一关系 - How to configure a one-to-many/many-to-one relationship between versioned entities C#LINQ与实体一对多关系:显示最后的日志条目数据时出现问题 - C# LINQ to Entities One-To-Many relationship: Problem displaying last log entry data 我是否需要使用EntityFramework更新一对多关系中的两个实体? - Do I need to update both Entities in a one-to-many relationship using EntityFramework? 如何使3个实体之间的一对多关系与Entity Framework Core 2.1一起使用 - 代码优先 - How to make one-to-many relationship between 3 entities work with Entity Framework Core 2.1 - code-first DDD:用户聚合根与其他聚合中几乎所有实体之间的一对多关系 - DDD: one-to-many relationship between user aggregate root and almost all entities in other aggregates 同一实体之间的EF一对一和一对多 - EF One-To-One and One-To-Many between same entities 从一对多关系中检索实体(Odata) - Retrieving entities from a one to many relationship (Odata)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM