C# 实体框架禁用加载虚拟成员

C# 实体框架禁用加载虚拟成员,c#,entity-framework,datagridview,C#,Entity Framework,Datagridview,我已经用SSMS创建了一个数据库。然后我完成了整个C#项目,并用NuGet安装了EF。我希望EF为我创建所有的类和上下文,所以我首先从数据库中编写代码,它为我做到了这一点。例如,产品类如下所示: public partial class Product { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsIn

我已经用SSMS创建了一个数据库。然后我完成了整个C#项目,并用NuGet安装了EF。我希望EF为我创建所有的类和上下文,所以我首先从数据库中编写代码,它为我做到了这一点。例如,
产品
类如下所示:

    public partial class Product
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Product()
        {
            Orders = new HashSet<Order>();
        }

        public int ID { get; set; }

        [Required]
        [StringLength(50)]
        public string Name { get; set; }

        public int Type_ID { get; set; }

        public decimal Price { get; set; }

        public string Descryption { get; set; }

        public int Available_amount { get; set; }

        public virtual Product_Type Product_Type { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Order> Orders { get; set; }
    }
}
其效果是:


首先,它把错误扔到我的脸上,所以我不得不加上这条线
this.Configuration.ProxyCreationEnabled=false在my
SkleContext
构造函数中


问题是我能做些什么使它不读取那些虚拟成员(不知道为什么EF会首先添加它们)或使EF生成那些没有它们的类(我不能仅仅通过删除
Product
class中的那两行来删除它们),所以我的
dataGridView
只显示数据库中的值?

如果你突然说

我只是不想在将数据绑定到dataGridView时使用最后两列

这两列出现在
dataGridView
中的原因是您直接绑定了模型

以下是一些备选方案:

1.绑定后移除列

dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);
dataGridView1.Columns.RemoveAt(dataGridView1.Columns.Count - 1);
2.为绑定创建不同的视图模型

public class DataGridViewModel
{   
    public int ID { get; set; }

    public string Name { get; set; }

    public int Type_ID { get; set; }

    public decimal Price { get; set; }

    public string Descryption { get; set; }

    public int Available_amount { get; set; }

    public DataGridViewModel()  
    {    
    }
}

public void GetProducts()
{
    using (var db = new SklepContext())
    {
        var data = db.Products.Select(r => new DataGridViewModel()
        {
            ID  = r.ID,
            Name = r.Name,
            Type_ID = r.Type_ID,
            Price = r.Price,
            Descryption = r.Descryption,
            Available_amount = r.Available_amount
        }).ToList();
        dataGridViewBrowse.DataSource = data;
    }
}

这里列举了几个选项,但我看不出有哪一个可以解决这个问题:(“这是在给我丢脸”。这是什么意思?为什么要将
ProxyCreationEnabled
设置为
false
?只看到错误,必须单击
Ok
添加每一行。在将数据绑定到
dataGridView
时,我不想要最后两列。我已经做了第二件事,但非常感谢第一件事!
public class DataGridViewModel
{   
    public int ID { get; set; }

    public string Name { get; set; }

    public int Type_ID { get; set; }

    public decimal Price { get; set; }

    public string Descryption { get; set; }

    public int Available_amount { get; set; }

    public DataGridViewModel()  
    {    
    }
}

public void GetProducts()
{
    using (var db = new SklepContext())
    {
        var data = db.Products.Select(r => new DataGridViewModel()
        {
            ID  = r.ID,
            Name = r.Name,
            Type_ID = r.Type_ID,
            Price = r.Price,
            Descryption = r.Descryption,
            Available_amount = r.Available_amount
        }).ToList();
        dataGridViewBrowse.DataSource = data;
    }
}