为什么我的实体框架没有删除我的记录?

时间:2012-08-13 21:38:12

标签: c# data-binding entity-framework-4 entity-framework-4.1

实体框架的新手,但我认为这应该很简单。

我的表单加载会从我的实体创建一个上下文。我创建了一个客户列表,并有一个我为客户分配的绑定源。绑定源被分配给绑定导航器 - clientBindingNavigator。

private void ClientExtForm_Load (object sender, EventArgs e)
        {

        _context = new IDVisitorEntities ();

        List<IDVM.Client> clients = _context.Clients.ToList ();

        clientBindingSource.DataSource = clients;

        }

摘自ClientExtForm.Designer.cs

        //
        // clientBindingNavigator
        // 
        this.clientBindingNavigator.AddNewItem = this.bindingNavigatorAddNewItem;
        this.clientBindingNavigator.BindingSource = this.clientBindingSource;
        this.clientBindingNavigator.CountItem = this.bindingNavigatorCountItem;
        this.clientBindingNavigator.DeleteItem = this.bindingNavigatorDeleteItem;
        this.clientBindingNavigator.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.bindingNavigatorMoveFirstItem,
        this.bindingNavigatorMovePreviousItem,
        this.bindingNavigatorSeparator,
        this.bindingNavigatorPositionItem,
        this.bindingNavigatorCountItem,
        this.bindingNavigatorSeparator1,
        this.bindingNavigatorMoveNextItem,
        this.bindingNavigatorMoveLastItem,
        this.bindingNavigatorSeparator2,
        this.bindingNavigatorAddNewItem,
        this.bindingNavigatorDeleteItem,
        this.clientBindingNavigatorSaveItem});

当我单击导航器工具栏上的“删除”按钮时,ClientBindingSource.Count减少了1。

private void clientBindingNavigatorSaveItem_Click (object sender, EventArgs e)
        {
        this.OnSave ();
        }



public override void OnSave ()
        {

        foreach (ObjectStateEntry entry in _context.ObjectStateManager.GetObjectStateEntries (EntityState.Deleted))
            {
           // nothing shows up in this
            }            
        foreach (ObjectStateEntry entry in _context.ObjectStateManager.GetObjectStateEntries (EntityState.Modified))
            {
            // when modified 
            }

        foreach (ObjectStateEntry entry in _context.ObjectStateManager.GetObjectStateEntries (EntityState.Added))
            {
            // when adding this finds it                    
            }

        clientBindingSource.EndEdit ();

        visitorHostsBindingSource.EndEdit ();

        _context.SaveChanges ();

        base.OnSave (); 
        }

好像导航器正在从集合中删除该项目。

添加信息:在导航器中,DeleteItem按钮对应于RemoveCurrent方法(在点击事件上调用它)。不知道在RemoveCurrent做之前如何配合。

我有什么选择来执行删除?

3 个答案:

答案 0 :(得分:0)

从clientBindingSource中删除项目对数据库级别的项目没有影响。您必须显式调用_context.Clients.DeleteObject(deletedClient);您必须通过ObjectContext执行所有CRUD操作。

答案 1 :(得分:0)

浏览后发现一些建议不使用默认DeleteItem的博客。

this.clientBindingNavigator.DeleteItem = null; // = this.bindingNavigatorDeleteItem;

在我的情况下,为了明确BindingNavigator,我在Items列表中用一个新按钮 this.toolStripButton1 替换了this.bindingNavigatorDeleteItem。

this.clientBindingNavigator.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.bindingNavigatorMoveFirstItem,
        this.bindingNavigatorMovePreviousItem,
        this.bindingNavigatorSeparator,
        this.bindingNavigatorPositionItem,
        this.bindingNavigatorCountItem,
        this.bindingNavigatorSeparator1,
        this.bindingNavigatorMoveNextItem,
        this.bindingNavigatorMoveLastItem,
        this.bindingNavigatorSeparator2,
        this.bindingNavigatorAddNewItem,
        this.toolStripButton1,
        this.clientBindingNavigatorSaveItem});

创建新按钮如下所示:

        // 
        // toolStripButton1
        // 
        this.toolStripButton1.Image = ((System.Drawing.Image) (resources.GetObject ("bindingNavigatorDeleteItem.Image")));
        this.toolStripButton1.RightToLeftAutoMirrorImage = true;
        this.toolStripButton1.Name = "toolStripDeleteItem";
        this.toolStripButton1.Size = new System.Drawing.Size(23, 22);
        this.toolStripButton1.Text = "Delete";
        this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);

Click事件然后调用RemoveCurrent(就像默认情况一样)但我可以获取当前实体并将其存储在arrraylist中以便在保存时使用。

private void toolStripButton1_Click (object sender, EventArgs e)
        {
        var currentclient = (Client) clientBindingSource.Current;
        clientstodelete.Add (currentclient);
        clientBindingSource.RemoveCurrent ();
        }

我不需要创建一个新按钮,我只需要将this.clientBindingNavigator.DeleteItem绑定到一个按钮。因为DeleteItem在引擎下创建了一个调用BindingSource.RemoveCurrent()的click事件。我可能会将按钮更改回创建的默认按钮,但为了说明,每个人都希望看到发生了什么。

答案 2 :(得分:0)

我同意,如果我刚刚使用RemoveCurrent()删除了,那么直接从表中删除记录似乎很奇怪。但事实就是如此......这样就可以完成数据网格和数据源的记录。

以下是我解决问题的方法:

        t_StaffDaysOff sdo = (t_StaffDaysOff)t_StaffDaysOffbindingSource.Current;
        t_StaffDaysOffbindingSource.RemoveCurrent();
        t_StaffDaysOffbindingSource.EndEdit();
        db.t_StaffDaysOff.Remove(sdo);
        db.SaveChanges();