C# 如何将源绑定到没有对象的实体

C# 如何将源绑定到没有对象的实体,c#,devexpress,entity-framework-6,C#,Devexpress,Entity Framework 6,对不起,我不知道如何解释标题中的问题 在设计模式下,当我将BindingSource绑定到实体类时,源没有对象(非null)。 我想在运行时模式下做同样的事情;绑定到同一类型或w/e BindingSource.DataSource = new entity(); // doesn't work because it's not clear BindingSource.DataSource = new EntityCollection<entity>() //it's clear,

对不起,我不知道如何解释标题中的问题

在设计模式下,当我将BindingSource绑定到实体类时,源没有对象(非null)。 我想在运行时模式下做同样的事情;绑定到同一类型或w/e

BindingSource.DataSource = new entity(); // doesn't work because it's not clear
BindingSource.DataSource = new EntityCollection<entity>() //it's clear, but is not the original type

(由于绑定错误,绑定到null不起作用……如果我使用bindingSource.Clear()也不起作用,因为当我调用ctx.Savechanges()时它删除所有实体

我建议您从使用开始。它提供了将网格控件绑定到任何数据源的简单直观的方法。例如,使用实体框架数据源,您可以通过BindingSource组件或使用DevEx提供的特殊数据绑定组件将网格控件直接绑定到EF数据源按,以或以某种方式进行装订,可使用或不使用

根据此向导,基于BindingSource的方法如下所示:

// This line of code is generated by Data Source Configuration Wizard
// Instantiate a new DBContext
NorthwindEntities dbContext = new NorthwindEntities();
// Call the Load method to get the data for the given DbSet from the database.
dbContext.Orders.Load();
// This line of code is generated by Data Source Configuration Wizard
bindingSource.DataSource = dbContext.Orders.Local.ToBindingList();
它还在
InitializeComponent()
方法中生成以下行,以提供管理所有GridControl设计时设置的功能:

BindingSource.DataSource = typeof(Entity)

您好,我正在使用ObjectContext…我一直在尝试对ObjectContext执行相同的操作,但它说没有可用的数据源…您知道在设计模式下绑定时使用的原始实体类型是什么吗?因为我希望在运行时使用相同的类型,但我找不到方法。在设计时,请尝试
BindingSource.datasource=typeof(Entity)
。在运行时使用我在回答中提到的方法。或者升级到最新版本的EF(DbContext)BindingSource.DataSource=typeof(Entity)!!!非常好,这就是我想要的,现在我可以正确验证我的所有表单了!谢谢
BindingSource.DataSource = typeof(Entity)