类的每个新实例的列表

本文关键字:实例 列表 新实例 | 更新日期: 2023-09-27 18:16:43

我有一个c#类teams .cs。当我创建一个团队时,我这样填充Event列表:

team.Events = db.Events.Where(sc => sc.SchoolcupEvent == true).ToList();

如果我只创建一个团队,它工作得很好,但当我创建第二个团队时,它看起来像第一个团队的事件列表又空了,有人知道为什么会发生这种情况吗?

你可以在下面找到我的teamcreate的代码。

Team.cs:

public class Team {
[Key]
[Required(ErrorMessage = "A name is required")]
public string TeamId { get; set; }
[DisplayName("Photo")]
public string Photo { get; set; }
[Display(Name = "Logo")]
public string Logo { get; set; }

[Required(ErrorMessage = "A sports type is required")]
public string Sport { get; set; }
public int CoachId { get; set; }
[ForeignKey("CoachId")]
public virtual Coach Coach { get; set; }
public string SchoolId { get; set; }
[ForeignKey("SchoolId")]
public virtual School School { get; set; }
public double Score { get; set; }
public List<Event> Events { get; set; }

[Required(ErrorMessage="A gender category is required")]
public string Gender { get; set; }

}

创建团队:

public ActionResult Create(Team team, IEnumerable<HttpPostedFileBase> files)
{
  Coach loggedCoach = new DBUserFinder().getLoggedCoach();
  team.CoachId = loggedCoach.CoachId;
  team.SchoolId = loggedCoach.SchoolId;
  team.Events = db.Events.Where(sc => sc.SchoolcupEvent == true).ToList();
  ViewBag.Type = new SelectList(db.Sports, "SportName", "SportName");
  if (ModelState.IsValid)
  {
    int i = 0;
    foreach (HttpPostedFileBase file in files)
    {
      if (file != null && file.ContentLength > 0)
      {
        string fileName = Path.GetFileName(file.FileName);
        var extention = Path.GetExtension(file.FileName);
        var allowedExtensions = new[] { ".png", ".jpg", ".jpeg" };
        if (allowedExtensions.Contains(extention))
        {
          fileName = team.TeamId.ToString();
          String fullPath = "";
          if (i == 0)
          {
            fullPath = fileName + " photo" + extention;
          }
          if (i == 1)
          {
            fullPath = fileName + " logo" + extention;
          }
          var path = Path.Combine(Server.MapPath("~/Content/Images/IO/Team"), fullPath);
          file.SaveAs(path);
          if (i == 0)
          {
            team.Photo = "../../Content/Images/IO/Team/" + team.TeamId + " photo" + extention;
          }
          if (i == 1)
          {
            team.Logo = "../../Content/Images/IO/Team/" + team.TeamId + " logo" + extention;
          }
        }
        i++;
      }
    }
    db.Teams.Add(team);
    db.SaveChanges();
    return RedirectToAction("Index");
  }

类的每个新实例的列表

您没有显示Event的代码,但看起来您正在建模团队将事件作为一对多,而它应该是多对多。

如果您将事件类更改为以下内容,它将将其建模为M2M。

public class Event
{
    public int EventId { get; set; } 
    public string EventName { get; set; }
    // .....all your other properties
    public virtual ICollection<Team> Teams { get; set; }  // this along with the List<Event> property in Team will result in a M2M relationship
}

这应该会为您创建一个EventTeams映射表。我的迁移文件看起来像这样:

CreateTable(
    "dbo.EventTeams",
    c => new
        {
            Event_EventId = c.Int(nullable: false),
            Team_TeamId = c.String(nullable: false, maxLength: 128),
        })
    .PrimaryKey(t => new { t.Event_EventId, t.Team_TeamId })
    .ForeignKey("dbo.Events", t => t.Event_EventId, cascadeDelete: true)
    .ForeignKey("dbo.Teams", t => t.Team_TeamId, cascadeDelete: true)
    .Index(t => t.Event_EventId)
    .Index(t => t.Team_TeamId);

另外,在您的团队模型中弹出virtual以允许延迟加载:

public virtual List<Event> Events { get; set; }

HTH