简体   繁体   中英

How to remove a line from `detail`. (Master detail)?

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:

在此处输入图片说明

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.
在此处输入图片说明

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

在此处输入图片说明 在此处输入图片说明

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM