实体框架和 DataNavigator

发布于 2024-11-07 12:30:58 字数 1337 浏览 0 评论 0原文

是否有人设法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2024-11-14 12:30:58

我认为您应该使用 BindingSource 控件而不是使用 DataSource 并处理 AddingNew 事件以手动将 State 设置为 Added调用AddObject

DataGrid 对数据源一无所知,因此它无法与实体框架上下文/设置和添加对象进行通信。您必须手动执行此操作,为此您需要在添加新记录时处理一些事件。我相信 AddingNewBindingSource 是可行的方法。

I think you should use BindingSource control instead of using DataSource and handle AddingNew event to manually set State to Added by calling AddObject.

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 and BindingSource is way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文