2013-04-03 152 views
0

喜用的ICollection我学习使用MVC使用代码第一种方法。我无法理解使用我的设置使用ICollection <>。我有两个类/实体来说:“餐厅” &“RestaurantReview”如下:与MVC与实体框架

public class Restaurant 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string City { get; set; } 
    public string Country { get; set; } 
    public ICollection<RestaurantReview> Reviews { get; set; } 
} 

和.....

public class RestaurantReview 
{ 
    public int Id { get; set; }  
    public int Rating { get; set; } 
    public string Body { get; set; } 
    public string ReviewerName { get; set; } 
    public int RestaurantId { get; set; } 


} 

现在什么困惑我是餐厅类的最后一个属性。为什么它是'ICollection'类型,并使用我的RestaurantReview类作为参数,它是做什么的,希望我 已经使我自己清楚

回答

0

这是一对多关系的定义。

有了该属性(有时称为导航属性)实体框架将能够将ReviewRestaurant连接起来。它也可以让你真的很容易得到Review实体给定Restaurant实体。

您还可以从RestaurantReview类中删除public int RestaurantId { get; set; }类 - 该列将由EF自动生成,因为ICollection<RestaurantReview>Restaurant类中。