当我从detail
表中删除一行时,仅删除外键的值-id_group
在存储表(SQL Server)中,并且行本身保留在表中。
我使用:
我使用实体框架连接到数据库。 创建了数据源。
拖到表单表数据源上
bindingSource
(bs_tbl_01_Groups
)Master
表的GridControl用一种方法填充。
cntDB.tbl_01_Groups.Load();
bs_tbl_01_Groups.DataSource = cntDB.tbl_01_Groups.Local.ToBindingList();
bindingSource
(bs_tbl03GroupsStud
)自动填充Detail
表的GridControl。
填充bindingSource
表的bs_tbl03GroupsStud
(Detail
)GridControl。
我想删除detail
表中的一行。
我尝试一下方法:
-gridView2.DeleteSelectedRows ();
;
或
-gridView2.DeleteRow (gridView2.FocusedRowHandle);
;
逻辑:
-用户。选择一个字符串;
-用户。按下Delete键;
运行代码-gridView2.DeleteSelectedRows();
-数据库中的行未删除;
-用户。按下Save
按钮;
代码已执行-cntDB.SaveChanges ();
-在数据库中,行中仅删除值FK
;
结果:
-方法仅删除存储表(MS SQL)中外键id_group
的值,而字符串本身仍保留在表中;
-仅从gridView
(gridControl
)中删除该行(这是因为链接的行中没有外键值);
问题::如何确保在从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 ()
方法
答案 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