c# - 防止数据网格在 C# 中加载 Entity Framework 导航属性

标签 c# winforms entity-framework entity-framework-6

我有一个数据库(具体来说是 Firebird,但我认为问题与 EF 有关)。我已经使用数据库中的 Code First 生成了代码文件。例如,为驱动程序生成的代码是:

[Table("Firebird.DRIVER")]
public partial class DRIVER
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public DRIVER()
    {
        FAREs = new HashSet<FARE>();
    //..... and other code generated for the other collections
    }


    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int DRIVERID { get; set; }

    [StringLength(100)]
    public string FULLNAME { get; set; }
    //... and other properties


    public int? EQUIPMENTID { get; set; }

    public virtual EQUIPMENT EQUIPMENT { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<FARE> FAREs { get; set; }
    //.... and other code generated for the other collections

 }

我有一个带有 DataGridView 的基本表单。 DataGridView 的目的是编辑、添加或删除新实体(驱动程序)。我是这样绑定(bind)的

    private async Task BindGrid()
    {
        var DBContext = AppVariables.CreateContext();
        await Task.Run(() => DBContext.DRIVERs.Load());
        var bindingSource = DBContext.DRIVERs.Local.ToBindingList();
        dataGridView1.DataSource = bindingSource;
    }

但是,我在我的网格中获得了导航属性(例如 EQUIPMENT)。我该怎么做才能从加载网格中删除导航属性。我应该先从代码中删除导航属性吗?或者是否有另一种方法可以在不加载导航属性的情况下将数据加载到网格中?

我曾尝试仅使用属性创建对象并避免导航,但网格不可编辑

    private async Task BindGrid()
    {
        var list = from driver in DBContext.DRIVERs
                   select new
                   {
                       DRIVERID = destination.DRIVERID
                       //...
                   };
        await list.LoadAsync();
        var bindingSource = list.ToBindingList();
        dataGridView1.DataSource = bindingSource;
    }

最佳答案

加载导航属性和显示它们是两个不同的主题。

阻止加载

禁用延迟加载,只包含那些你需要的导航属性

var db = new MyDbContext(); 
db.Configuration.LazyLoadingEnabled = false; 
data = db.MyEntity.Local.ToBindingList();

阻止显示

通过[Browsable(false)]装饰导航属性。

[Browsable(false)]
public virtual EQUIPMENT EQUIPMENT { get; set; }

或者

将列可见性设置为 false:

dataGridView1.Columns["EQUIPMENT"].Visible = false

或者

首先,使用设计器或使用代码为您的 DataGridView 定义一组您想要的列。然后 DataGridView 将只显示您定义的那些列:

var DRIVERIDColumn = new DataGridViewTextBoxColumn();
DRIVERIDColumn.Name = "DRIVERID";
DRIVERIDColumn.HeaderText = "Id";
DRIVERIDColumn.DataPropertyName= "DRIVERID";
// ...
dataGridView1.Columns.AddRange(DRIVERIDColumn /*...*/);
// ...

关于c# - 防止数据网格在 C# 中加载 Entity Framework 导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53848291/

相关文章:

c# - 为什么一个循环在内存和性能方面比其他循环表现更好?

winforms - 从 pdf 查看器中提取文本和所有字体信息

c# - MVVM 光 : Using DataService to retrieve database items

C# 代码片段不会自动导入命名空间

c# - 将char转换为整数表示c#

c# - 如何确定网页是否已完全加载到网络浏览器中?

asp.net-mvc - 使用瘦 Controller 的 N 层域/ View 模型的最佳实践

c# - Entity Framework v1 - 高流量网站高峰时段出现 "Timeout expired"异常

c# - 为什么 EventInfo.RemoveEventHandler 抛出 NullReferenceException?

c# - 如何将 DataGridView 与 SqlDataReader 绑定(bind)