Search code examples
c#data-bindingentity-framework-4entity-framework-4.1

Why isn't my entity framework deleting my record?


New to entity framework, but I would think this should be pretty simple.

My form load creates a context from my Entities. I create a list of clients and have a binding source that I assign clients to. The binding source is assigned to a Binding Navigator - clientBindingNavigator.

private void ClientExtForm_Load (object sender, EventArgs e)
        {

        _context = new IDVisitorEntities ();

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

        clientBindingSource.DataSource = clients;

        }

excerpt from 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});

When I click the Delete Button on the navigator tool bar the the ClientBindingSource.Count has been reduced by 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 (); 
        }

It appears as though the navigator is removing the item from the collection.

Added info: It appears that in the navigator the DeleteItem button corresponds to the RemoveCurrent method (on click event calls it). Not sure how to tie in before the RemoveCurrent does it's thing.

What are my options for preforming the delete?


Solution

  • After looking around found some blogs that suggest to not use the default DeleteItem.

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

    In my case to make it clear for the BindingNavigator I replaced this.bindingNavigatorDeleteItem with a new button this.toolStripButton1 in the Items list.

    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});
    

    Creation of the new button looks like this:

            // 
            // 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);
    

    The Click event then calls the RemoveCurrent (just like the default does) but I can get the current entity and stash it in an arrraylist for use on save.

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

    I didn't need to create a new button, I just needed to have the this.clientBindingNavigator.DeleteItem not tied to a button. Because the DeleteItem creates a click event under the hood that calls the BindingSource.RemoveCurrent(). I might change the button back to the default one created but for illustration wanted everyone to see what was happening.