数据类中引用virtual

    public class City
    {
        [Key]
        public int CityID { set; get; }
        [Display(Name = "城市名称")]
        [Required(ErrorMessage = "城市名不能为空")]
        public string CityName { set; get; }
        [Display(Name = "拼音码")]
        public string CityPy { set; get; }
        [Display(Name = "省份名称")]
        [Required(ErrorMessage = "所属省份不能为空")]
        public int ProvincwID { set; get; }
        public virtual Provincw Provincw { set; get; }
        [Display(Name = "邮编")]
        public string Postcode { set; get; }
        [Display(Name = "区号")]
        public string AreaCode { set; get; }

    }

关于Virtual,教程中的原文如下:

While we’re there, we’ve also changed the Genre and Artist to virtual properties. This allows Entity Framework to lazy-load them as necessary.

理解为:使用了virtual的类或类集合的好处在于延时加载,只有在使用该数据时,EF再去自动执行数据加载操作。

原文地址:https://www.cnblogs.com/wyq00789/p/6400128.html