开发者

Delete Sets Same Table FK's to NULL

开发者 https://www.devze.com 2023-03-10 02:31 出处:网络
I have a table in SQL Server: CREATE TABLE [dbo].[Account] ( [AccountID]NVARCHAR (20)NOT NULL, [ParentID]NVARCHAR (20)NULL

I have a table in SQL Server:

CREATE TABLE [dbo].[Account] 
(
[AccountID]    NVARCHAR (20)   NOT NULL,
[ParentID]     NVARCHAR (20)   NULL
);

Also there is the same table FK ParentID-->AccountID ParentID is either null or contains the parent node.

In SQL Server the refrential integrity works correctly: It doesn't allow deleting the parent record if a child record exists. In my Entity Framework model which I created from the database when I try to delete the parent row EF first sets the child's ParentIDs to NULL and then deletes the parent row:

        Account acc = new Account();

        acc = (Account)accountListBox.SelectedItem;

        _context.DeleteObject(acc);

        _context.SaveChanges();

This 开发者_如何学运维is obviously not what I would have expected. Is there something wrong with my model? How can I enforce the ref. integrity in this case?

0

精彩评论

暂无评论...
验证码 换一张
取 消