简体   繁体   English

将ICollection与MVC和实体框架结合使用

[英]Using ICollection with MVC & Entity framework

Hi I'm learning to use MVc using Code first method. 嗨,我正在学习使用“代码优先”方法使用MVc。 I'm having trouble understanding the use of ICollection<> with my setup. 我在理解安装程序中对ICollection <>的使用时遇到了麻烦。 I have two classes/entitys: 'Restaurant' & 'RestaurantReview' as below: 我有两个类/实体:“餐厅”和“餐厅评论”,如下所示:

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

and..... 和.....

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


}

now whats confusing me is the last property of the Restaurant class. 现在,令我感到困惑的是Restaurant类的最后一个属性。 Why is it of type 'ICollection' and uses my RestaurantReview class as a parameter, what does it do, hope I have made myself clear 为什么它是“ ICollection”类型,并使用我的RestaurantReview类作为参数,它的作用是,我希望自己弄清楚

It's a definition of one to many relationship. 这是一对多关系的定义。

With that property (sometimes called Navigation Property ) Entity Framework will be able to connect Review with Restaurant it was written about. 使用该属性(有时称为Navigation Property ),实体框架将能够将Review与它所涉及的Restaurant连接起来。 It will also allow you to get Review entities for given Restaurant entity really easily. 它还将使您真正轻松地获得给定Restaurant实体的Review实体。

You can also remove public int RestaurantId { get; set; } 您也可以删除public int RestaurantId { get; set; } public int RestaurantId { get; set; } public int RestaurantId { get; set; } from RestaurantReview class - That column is gonna be generated automatically by EF because of ICollection<RestaurantReview> in Restaurant class. public int RestaurantId { get; set; } from RestaurantReview类-由于Restaurant类中的ICollection<RestaurantReview> ,该列将由EF自动生成。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM