列出每个类的新实例

时间:2013-06-03 23:24:23

标签: c# asp.net-mvc

我有一个C#类Team.cs.当我创建一个团队时,我通过这样做来填充事件列表:

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");
  }

1 个答案:

答案 0 :(得分:1)

您没有显示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