How does WillCascadeOnDelete in Entity Framework work?
As I understand, if I delete a parent row, its children should be deleted if I turn on cascade on delete. However, from my testing, it doesn't seem to work at all. No matter if I set WillCascaseOnDelete to true or false, it simply sets the foreign key of its children to null. This is causing another problem that I have to set the foreign key nullable, otherwise, SaveChange will throw exception. Is this a defect or desired behavior?
entity-framework cascade
add a comment |
As I understand, if I delete a parent row, its children should be deleted if I turn on cascade on delete. However, from my testing, it doesn't seem to work at all. No matter if I set WillCascaseOnDelete to true or false, it simply sets the foreign key of its children to null. This is causing another problem that I have to set the foreign key nullable, otherwise, SaveChange will throw exception. Is this a defect or desired behavior?
entity-framework cascade
add a comment |
As I understand, if I delete a parent row, its children should be deleted if I turn on cascade on delete. However, from my testing, it doesn't seem to work at all. No matter if I set WillCascaseOnDelete to true or false, it simply sets the foreign key of its children to null. This is causing another problem that I have to set the foreign key nullable, otherwise, SaveChange will throw exception. Is this a defect or desired behavior?
entity-framework cascade
As I understand, if I delete a parent row, its children should be deleted if I turn on cascade on delete. However, from my testing, it doesn't seem to work at all. No matter if I set WillCascaseOnDelete to true or false, it simply sets the foreign key of its children to null. This is causing another problem that I have to set the foreign key nullable, otherwise, SaveChange will throw exception. Is this a defect or desired behavior?
entity-framework cascade
entity-framework cascade
edited Nov 20 at 11:02
Rowland Shaw
32.2k1181146
32.2k1181146
asked Jul 29 '13 at 19:37
newman
3,1931060100
3,1931060100
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This is because your foreign keys (child) are nullable. By default, when deleting parent, if the foreign key on the relationship is nullable EF will delete the parent and set the foreign key to null. If the foreign key is NOT NULL it will delete the child (the behaviour you're looking for?).
You can alter this default behaviour here
3
I'm experiencing same problem, my Foreign Key is set asIsRequired();
, the relationship has.WillCascadeOnDelete(true)
but I still get an Exception when trying to delete the parent object.
– Steven Ryssaert
Jul 19 '14 at 9:02
@UwConcept, I would expect an exception to be thrown if you've specified the foreign-key relationship asIsRequired();
as Entity Framework cannot set the value tonull
following removal of the parent object.
– weenoid
Oct 5 '15 at 10:42
3
@weenoid Thanks for your reply. But wouldn't it make more sense to then delete the child object instead of setting a NULL value in a non nullable field? Just my two cents.
– Steven Ryssaert
Oct 6 '15 at 9:01
1
@StevenRyssaert Entity framework will delete such objects. Just make sure that those are loaded into the context via Include or LazyLoading. EntityFramework cannot mark object as deleted if it doesn't know that this object exists
– Machet
Sep 23 at 15:20
add a comment |
Make sure on the Foreign Key Relationship window in SQL Server, you have selected Cascade as Delete rule.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17932720%2fhow-does-willcascadeondelete-in-entity-framework-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is because your foreign keys (child) are nullable. By default, when deleting parent, if the foreign key on the relationship is nullable EF will delete the parent and set the foreign key to null. If the foreign key is NOT NULL it will delete the child (the behaviour you're looking for?).
You can alter this default behaviour here
3
I'm experiencing same problem, my Foreign Key is set asIsRequired();
, the relationship has.WillCascadeOnDelete(true)
but I still get an Exception when trying to delete the parent object.
– Steven Ryssaert
Jul 19 '14 at 9:02
@UwConcept, I would expect an exception to be thrown if you've specified the foreign-key relationship asIsRequired();
as Entity Framework cannot set the value tonull
following removal of the parent object.
– weenoid
Oct 5 '15 at 10:42
3
@weenoid Thanks for your reply. But wouldn't it make more sense to then delete the child object instead of setting a NULL value in a non nullable field? Just my two cents.
– Steven Ryssaert
Oct 6 '15 at 9:01
1
@StevenRyssaert Entity framework will delete such objects. Just make sure that those are loaded into the context via Include or LazyLoading. EntityFramework cannot mark object as deleted if it doesn't know that this object exists
– Machet
Sep 23 at 15:20
add a comment |
This is because your foreign keys (child) are nullable. By default, when deleting parent, if the foreign key on the relationship is nullable EF will delete the parent and set the foreign key to null. If the foreign key is NOT NULL it will delete the child (the behaviour you're looking for?).
You can alter this default behaviour here
3
I'm experiencing same problem, my Foreign Key is set asIsRequired();
, the relationship has.WillCascadeOnDelete(true)
but I still get an Exception when trying to delete the parent object.
– Steven Ryssaert
Jul 19 '14 at 9:02
@UwConcept, I would expect an exception to be thrown if you've specified the foreign-key relationship asIsRequired();
as Entity Framework cannot set the value tonull
following removal of the parent object.
– weenoid
Oct 5 '15 at 10:42
3
@weenoid Thanks for your reply. But wouldn't it make more sense to then delete the child object instead of setting a NULL value in a non nullable field? Just my two cents.
– Steven Ryssaert
Oct 6 '15 at 9:01
1
@StevenRyssaert Entity framework will delete such objects. Just make sure that those are loaded into the context via Include or LazyLoading. EntityFramework cannot mark object as deleted if it doesn't know that this object exists
– Machet
Sep 23 at 15:20
add a comment |
This is because your foreign keys (child) are nullable. By default, when deleting parent, if the foreign key on the relationship is nullable EF will delete the parent and set the foreign key to null. If the foreign key is NOT NULL it will delete the child (the behaviour you're looking for?).
You can alter this default behaviour here
This is because your foreign keys (child) are nullable. By default, when deleting parent, if the foreign key on the relationship is nullable EF will delete the parent and set the foreign key to null. If the foreign key is NOT NULL it will delete the child (the behaviour you're looking for?).
You can alter this default behaviour here
answered Sep 25 '13 at 16:05
Matt Cotton
424719
424719
3
I'm experiencing same problem, my Foreign Key is set asIsRequired();
, the relationship has.WillCascadeOnDelete(true)
but I still get an Exception when trying to delete the parent object.
– Steven Ryssaert
Jul 19 '14 at 9:02
@UwConcept, I would expect an exception to be thrown if you've specified the foreign-key relationship asIsRequired();
as Entity Framework cannot set the value tonull
following removal of the parent object.
– weenoid
Oct 5 '15 at 10:42
3
@weenoid Thanks for your reply. But wouldn't it make more sense to then delete the child object instead of setting a NULL value in a non nullable field? Just my two cents.
– Steven Ryssaert
Oct 6 '15 at 9:01
1
@StevenRyssaert Entity framework will delete such objects. Just make sure that those are loaded into the context via Include or LazyLoading. EntityFramework cannot mark object as deleted if it doesn't know that this object exists
– Machet
Sep 23 at 15:20
add a comment |
3
I'm experiencing same problem, my Foreign Key is set asIsRequired();
, the relationship has.WillCascadeOnDelete(true)
but I still get an Exception when trying to delete the parent object.
– Steven Ryssaert
Jul 19 '14 at 9:02
@UwConcept, I would expect an exception to be thrown if you've specified the foreign-key relationship asIsRequired();
as Entity Framework cannot set the value tonull
following removal of the parent object.
– weenoid
Oct 5 '15 at 10:42
3
@weenoid Thanks for your reply. But wouldn't it make more sense to then delete the child object instead of setting a NULL value in a non nullable field? Just my two cents.
– Steven Ryssaert
Oct 6 '15 at 9:01
1
@StevenRyssaert Entity framework will delete such objects. Just make sure that those are loaded into the context via Include or LazyLoading. EntityFramework cannot mark object as deleted if it doesn't know that this object exists
– Machet
Sep 23 at 15:20
3
3
I'm experiencing same problem, my Foreign Key is set as
IsRequired();
, the relationship has .WillCascadeOnDelete(true)
but I still get an Exception when trying to delete the parent object.– Steven Ryssaert
Jul 19 '14 at 9:02
I'm experiencing same problem, my Foreign Key is set as
IsRequired();
, the relationship has .WillCascadeOnDelete(true)
but I still get an Exception when trying to delete the parent object.– Steven Ryssaert
Jul 19 '14 at 9:02
@UwConcept, I would expect an exception to be thrown if you've specified the foreign-key relationship as
IsRequired();
as Entity Framework cannot set the value to null
following removal of the parent object.– weenoid
Oct 5 '15 at 10:42
@UwConcept, I would expect an exception to be thrown if you've specified the foreign-key relationship as
IsRequired();
as Entity Framework cannot set the value to null
following removal of the parent object.– weenoid
Oct 5 '15 at 10:42
3
3
@weenoid Thanks for your reply. But wouldn't it make more sense to then delete the child object instead of setting a NULL value in a non nullable field? Just my two cents.
– Steven Ryssaert
Oct 6 '15 at 9:01
@weenoid Thanks for your reply. But wouldn't it make more sense to then delete the child object instead of setting a NULL value in a non nullable field? Just my two cents.
– Steven Ryssaert
Oct 6 '15 at 9:01
1
1
@StevenRyssaert Entity framework will delete such objects. Just make sure that those are loaded into the context via Include or LazyLoading. EntityFramework cannot mark object as deleted if it doesn't know that this object exists
– Machet
Sep 23 at 15:20
@StevenRyssaert Entity framework will delete such objects. Just make sure that those are loaded into the context via Include or LazyLoading. EntityFramework cannot mark object as deleted if it doesn't know that this object exists
– Machet
Sep 23 at 15:20
add a comment |
Make sure on the Foreign Key Relationship window in SQL Server, you have selected Cascade as Delete rule.
add a comment |
Make sure on the Foreign Key Relationship window in SQL Server, you have selected Cascade as Delete rule.
add a comment |
Make sure on the Foreign Key Relationship window in SQL Server, you have selected Cascade as Delete rule.
Make sure on the Foreign Key Relationship window in SQL Server, you have selected Cascade as Delete rule.
answered Feb 28 '14 at 23:29
asr
43149
43149
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17932720%2fhow-does-willcascadeondelete-in-entity-framework-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown