实体框架和 DataNavigator
是否有人设法使用 DataSource 属性和 ADO.NET 实体框架绑定 DataNavigator 和 DataGrid,以便添加和删除(数据导航器中的 + 和 - 按钮)。工作?我有一个问题,每次我单击 DataNavigator 的添加按钮时,EntityState 总是设置为分离。我不知道如何将这个独立的实体添加到 DataContext 中。
我的代码很简单(使用静态会话类和部分类):
internal class Session
{
private static Entities _entities;
public static Entities Entities
{
get { return _entities ?? (_entities = new Entities()); }
set { _entities = value; }
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitData();
}
private void InitData()
{
gridControl1.DataSource = Session.Entities.SomeObjects;
dataNavigator1.DataSource = Session.Entities.SomeObjects;
}
}
public partial class SomeObjects
{
public SomeObjects()
{
PropertyChanged += SomeObject_PropertyChanged;
ObjectId = Guid.NewGuid();
}
private void SomeObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Debug.WriteLine(EntityState); // when i change a existing record in the grid, EntityState is set to modified and can be saved easily using SaveChanges. But when i add a new entity, EntityState is always set to detached.
}
}
感谢帮助!
-基督教
Has anyone managed to bind a DataNavigator and a DataGrid using the DataSource property and the ADO.NET Entity Framework so that add and delete (+ and - buttons in the data navigator). work? I have the problem, that everytime i click on the add button of the DataNavigator, the EntityState is always set to detached. I can't figure out, how to add this detached entity to the DataContext.
my code is simple (using a static session class and a partial class):
internal class Session
{
private static Entities _entities;
public static Entities Entities
{
get { return _entities ?? (_entities = new Entities()); }
set { _entities = value; }
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitData();
}
private void InitData()
{
gridControl1.DataSource = Session.Entities.SomeObjects;
dataNavigator1.DataSource = Session.Entities.SomeObjects;
}
}
public partial class SomeObjects
{
public SomeObjects()
{
PropertyChanged += SomeObject_PropertyChanged;
ObjectId = Guid.NewGuid();
}
private void SomeObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Debug.WriteLine(EntityState); // when i change a existing record in the grid, EntityState is set to modified and can be saved easily using SaveChanges. But when i add a new entity, EntityState is always set to detached.
}
}
Help's appreciated!
-christian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该使用
BindingSource
控件而不是使用DataSource
并处理AddingNew
事件以手动将 State 设置为Added
调用AddObject
。DataGrid 对数据源一无所知,因此它无法与实体框架上下文/设置和添加对象进行通信。您必须手动执行此操作,为此您需要在添加新记录时处理一些事件。我相信
AddingNew
和BindingSource
是可行的方法。I think you should use
BindingSource
control instead of usingDataSource
and handleAddingNew
event to manually set State toAdded
by callingAddObject
.DataGrid doesn't know anything about source of data so it cannot communicate with entity framework context / set and add object. You must do it manually and for that you need some event to handle when new record is added. I believe
AddingNew
andBindingSource
is way to go.