1

When I delete a row from the detail table, only the value of the foreign key is deleted - id_group, in the storage table (SQL Server), and the row itself remains in the table.
  I use:

  • SQL Server;
  • Entity Framework;
  • devexpress;
  • WinForms;
  • gridControl

I connected to the database using Entity Framework. Created a data source.

Dragged onto the form table data source:

enter image description here

bindingSource (bs_tbl_01_Groups) The GridControl of the Master table is populated with a method.

cntDB.tbl_01_Groups.Load();
bs_tbl_01_Groups.DataSource = cntDB.tbl_01_Groups.Local.ToBindingList();

bindingSource (bs_tbl03GroupsStud) The GridControl of the Detail table is automatically populated.
Filling the bindingSource (bs_tbl03GroupsStud) GridControl of the Detail table.
enter image description here

I want to delete a row in the detail table.
I try the methods:
- gridView2.DeleteSelectedRows ();;
or
- gridView2.DeleteRow (gridView2.FocusedRowHandle);;
 

Logics:
  - User. Selects a string;
  - User. Press the Delete button;
Running code - gridView2.DeleteSelectedRows();
  - The row in the database is not deleted;
  - User. Press the Save button;
The code is executed - cntDB.SaveChanges ();
  - In the database, only the value FK is deleted in the row;

Result:
 - methods remove only the value of the foreign key id_group in the storage table (MS SQL), and the string itself remains in the table;
 - the line is deleted only from gridView (gridControl) (this happens because there is no foreign key value from the linked line);

enter image description here enter image description here

Question: How to ensure that when deleting a row from the detail table, the row is deleted from the storage table (SQL Server)?

Feature:
I had a problem: in the GridControl (detail) instead of a table, the columns" Count "and" Is Read Only "were displayed. Link
Solution: used the ObservableListSource.cs class. Link

Application code:

 ContextDB cntDB;
private void Form1_Load(object sender, EventArgs e)
{
   cntDB = new ContextDB();

    FillGrid();   
}

public void FillGrid()
{
    cntDB.tbl_01_Groups.Load();
    bs_tbl_01_Groups.DataSource = cntDB.tbl_01_Groups.Local.ToBindingList();            
}

public void RemoveRow_gridView_2()
{
    gridView2.DeleteSelectedRows();
}


public void Save()
{
    cntDB.SaveChanges();
}

The SQL code of the Master tables.

CREATE TABLE [dbo].[tbl_01_Groups] (
  [id_group] int  IDENTITY(1,1) NOT NULL,
  [nameGroup] nvarchar(255) COLLATE Cyrillic_General_CI_AS  NULL,
  [Property_1_Group] nvarchar(255) COLLATE Cyrillic_General_CI_AS  NULL,
  [Property_2_Group] nvarchar(255) COLLATE Cyrillic_General_CI_AS  NULL,
  [Property_3_Group] nvarchar(255) COLLATE Cyrillic_General_CI_AS  NULL,
  CONSTRAINT [PK_tbl_01_Groups] PRIMARY KEY NONCLUSTERED ([id_group])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = OFF, ALLOW_PAGE_LOCKS = OFF)  
ON [PRIMARY]
)  
ON [PRIMARY]
GO

ALTER TABLE [dbo].[tbl_01_Groups] SET (LOCK_ESCALATION = TABLE)

  The SQL code for the detail tables.

CREATE TABLE [dbo].[tbl_03_GroupsStud] (
  [id_groupStud] int  IDENTITY(1,1) NOT NULL,
  [id_group] int  NULL,
  [id_stud] int  NULL,
  [groupStud_descript] nvarchar(255) COLLATE Cyrillic_General_CI_AS  NULL,
  CONSTRAINT [PK_tbl_03_GroupsStud] PRIMARY KEY NONCLUSTERED ([id_groupStud])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = OFF, ALLOW_PAGE_LOCKS = OFF)  
ON [PRIMARY],
  CONSTRAINT [FK_id_grp] FOREIGN KEY ([id_group]) REFERENCES [dbo].[tbl_01_Groups] ([id_group]) ON DELETE NO ACTION ON UPDATE NO ACTION
)  
ON [PRIMARY]
GO

ALTER TABLE [dbo].[tbl_03_GroupsStud] SET (LOCK_ESCALATION = TABLE)

The entity framework code of the Master table.

public partial class tbl_01_Groups
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public tbl_01_Groups()
        {
            this.tbl_03_GroupsStud = new ObservableListSource<tbl_03_GroupsStud>();
        }

        public int id_group { get; set; }
        public string nameGroup { get; set; }
        public string Property_1_Group { get; set; }
        public string Property_2_Group { get; set; }
        public string Property_3_Group { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ObservableListSource<tbl_03_GroupsStud> tbl_03_GroupsStud { get; set; }
    }

  The entity framework code of the detail table.

public partial class tbl_03_GroupsStud
    {
        public int id_groupStud { get; set; }
        public Nullable<int> id_group { get; set; }
        public Nullable<int> id_stud { get; set; }
        public string groupStud_descript { get; set; }

        public virtual tbl_01_Groups tbl_01_Groups { get; set; }
    }

Update_1
SaveChanges () method

enter image description here

eusataf
  • 807
  • 1
  • 12
  • 24
  • Have you checked using a profiler what command is send to the database ? It sounds like no delete command is send but an update to clear the fk field only – GuidoG Apr 02 '19 at 14:37
  • `gridView2.DeleteRow` removes the entire row in my devexpress gridviews. The problem is in your code that should delete the line in the database. In stead of deleting it this code removes only the FK. Can you show the code that handles the delete ? – GuidoG Apr 02 '19 at 14:49
  • @GuidoG Updated question – eusataf Apr 02 '19 at 15:28
  • And what does `cntDB.SaveChanges();` do ? – GuidoG Apr 03 '19 at 07:04
  • @GuidoG **1.** Conservation of change. I do not understand well, but probably it only saves the "changes". If the line is deleted, it does not save changes or saves, as I described above in the question. **2.** Link to manual - [link](http://www.entityframeworktutorial.net/significance-of-savechanges.aspx) – eusataf Apr 03 '19 at 08:53
  • So you cant see or change the code of `SaveChanges` ? Because it really seems that the problem is there – GuidoG Apr 03 '19 at 09:19
  • @GuidoG Updated the question - "Update_1". Maybe yes. I can't change it. This is the `entity-framework` method, if I understood correctly. Although I do not understand well. – eusataf Apr 03 '19 at 09:36

1 Answers1

0

To clarify, you want a way to ensure that when a record in the master table gets deleted, all child records linked to it also gets deleted? This can easily be solved using an ON DELETE TRIGGER.

CREATE TRIGGER SampleTrigger
    ON [dbo].[tbl_01_Groups]
    FOR DELETE
AS
    DELETE FROM [dbo].[tbl_03_GroupsStud]
    WHERE id_group IN (SELECT deleted.id_group FROM deleted)
GO
Randolph
  • 302
  • 3
  • 12
  • Not. In GridControl (`detail`), the table` detail` (`tbl_03_GroupsStud`) is displayed. I want the line to be deleted in the `detail` (`tbl_03_GroupsStud`) table of the SQL server when deleting a row in the GridControl. If I understand correctly, you propose a solution: after deleting a row in GridControl (`master`), all related rows in GridControl (`detail`) are deleted, as well as all rows in the storage table (`detail`) SQL server If I could not explain, please ask more – eusataf Apr 02 '19 at 14:04
  • That is not his problem. His problem is that when he deletes a row from the GroupStud table, the row is not deleted from the database. Only the FK is updated to NULL – GuidoG Apr 02 '19 at 14:57