如何从`detail`中删除一行。 (主要细节)?

时间:2019-04-02 10:27:55

标签: c# sql-server winforms entity-framework devexpress

当我从detail表中删除一行时,仅删除外键的值-id_group在存储表(SQL Server)中,并且行本身保留在表中。
我使用:

  • SQL Server;
  • 实体框架;
  • devexpress;
  • WinForms;
  • gridControl

我使用实体框架连接到数据库。 创建了数据源。

拖到表单表数据源上

enter image description here

bindingSourcebs_tbl_01_GroupsMaster表的GridControl用一种方法填充。

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

bindingSourcebs_tbl03GroupsStud)自动填充Detail表的GridControl。
填充bindingSource表的bs_tbl03GroupsStudDetail)GridControl。
enter image description here

我想删除detail表中的一行。
我尝试一下方法:
-gridView2.DeleteSelectedRows ();;

-gridView2.DeleteRow (gridView2.FocusedRowHandle);;

逻辑:
-用户。选择一个字符串;
-用户。按下Delete键;
运行代码-gridView2.DeleteSelectedRows();
-数据库中的行未删除;
-用户。按下Save按钮;
代码已执行-cntDB.SaveChanges ();
-在数据库中,行中仅删除值FK

结果:
-方法仅删除存储表(MS SQL)中外键id_group的值,而字符串本身仍保留在表中;
-仅从gridViewgridControl)中删除该行(这是因为链接的行中没有外键值);

enter image description here enter image description here

问题::如何确保在从detail表中删除行时,从存储表(SQL Server)中删除该行?

功能
我遇到了一个问题:在GridControl(detail)中,而不是表格中,显示了“ Count”和“ Is Only Only”列。 Link
解决方案:使用了ObservableListSource.cs类。 Link

应用代码:

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

Master表的SQL代码。

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)

detail表的SQL代码。

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)

entity framework表的Master代码。

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

entity framework表的detail代码。

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

更新_1
SaveChanges ()方法

enter image description here

1 个答案:

答案 0 :(得分:0)

为澄清起见,您想要一种方法来确保当主表中的记录被删除时,与其链接的所有子记录也都被删除吗?使用 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