我刚刚开始使用EF代码,所以我在这个主题完全是一个初学者。

我想创建团队和比赛之间的关系:

1场比赛= 2支队伍(主队,客队)和结果。

我认为创建这样一个模型很容易,所以我开始编码:

public class Team
{
    [Key]
    public int TeamId { get; set;} 
    public string Name { get; set; }

    public virtual ICollection<Match> Matches { get; set; }
}


public class Match
{
    [Key]
    public int MatchId { get; set; }

    [ForeignKey("HomeTeam"), Column(Order = 0)]
    public int HomeTeamId { get; set; }
    [ForeignKey("GuestTeam"), Column(Order = 1)]
    public int GuestTeamId { get; set; }

    public float HomePoints { get; set; }
    public float GuestPoints { get; set; }
    public DateTime Date { get; set; }

    public virtual Team HomeTeam { get; set; }
    public virtual Team GuestTeam { get; set; }
}

我得到一个异常:

引用关系将导致不允许的循环引用。[约束名称= Match_GuestTeam]

我如何创建这样一个模型,有2个外键到同一个表?

我想保存我的编辑到数据库,我使用实体框架代码-在ASP。NET MVC 3 / c#,但我得到错误。在我的事件类中,我有DateTime和TimeSpan数据类型,但在我的数据库中,我分别有日期和时间。这就是原因吗?在将更改保存到数据库之前,如何在代码中转换为适当的数据类型。

public class Event
{
    public int EventId { get; set; }
    public int CategoryId { get; set; }
    public int PlaceId { get; set; }
    public string Title { get; set; }
    public decimal Price { get; set; }
    public DateTime EventDate { get; set; }
    public TimeSpan StartTime { get; set; }
    public TimeSpan EndTime { get; set; }
    public string Description { get; set; }
    public string EventPlaceUrl { get; set; }
    public Category Category { get; set; }
    public Place Place { get; set; }
}

方法>>>>在storeDB.SaveChanges()中的问题;

// POST: /EventManager/Edit/386        
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
    var theEvent = storeDB.Events.Find(id);

    if (TryUpdateModel(theEvent))
    {
        storeDB.SaveChanges();
        return RedirectToAction("Index");
    }
    else
    {
        ViewBag.Categories = storeDB.Categories.OrderBy(g => g.Name).ToList();
        ViewBag.Places = storeDB.Places.OrderBy(a => a.Name).ToList();
        return View(theEvent);
    }
}

public class EventCalendarEntities : DbContext
{
    public DbSet<Event> Events { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Place> Places { get; set; } 
}

SQL Server 2008 R2数据库

EventDate (Datatype = date)  
StartTime (Datatype = time)  
EndTime (Datatype = time)  

Http表单

EventDate (Datatype = DateTime) e.g. 4/8/2011 12:00:00 AM  
StartTime (Datatype = Timespan/time not sure) e.g. 08:30:00  
EndTime (Datatype = Timespan/time not sure) e.g. 09:00:00  

“/”应用程序中的服务器错误。

一个或多个实体验证失败。更多细节请参见'EntityValidationErrors'属性。

描述:在执行当前web请求期间发生了一个未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。

System.Data.Entity.Validation.DbEntityValidationException:一个或多个实体验证失败。更多细节请参见'EntityValidationErrors'属性。

源错误:

Line 75:             if (TryUpdateModel(theEvent))
Line 76:             {
Line 77:                 storeDB.SaveChanges();
Line 78:                 return RedirectToAction("Index");
Line 79:             }

源文件:C:\sep\MvcEventCalendar\MvcEventCalendar\Controllers\EventManagerController.cs

堆栈跟踪:

[DbEntityValidationException:一个或多个实体验证失败。]更多细节请参见'EntityValidationErrors'属性。]

我在教程中经常看到这种情况,导航属性为ICollection<T>。

这是实体框架的强制性要求吗?我可以使用IEnumerable吗?

使用ICollection而不是IEnumerable或List<T>的主要目的是什么?