Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullifying dependent field is not allowed for FK restrict #10542

Closed
Tarig0 opened this issue Dec 12, 2017 · 3 comments
Closed

Nullifying dependent field is not allowed for FK restrict #10542

Tarig0 opened this issue Dec 12, 2017 · 3 comments

Comments

@Tarig0
Copy link

Tarig0 commented Dec 12, 2017

I have a relationship that I would like to sever, but when I nullify the property in question I get the following error.

I would only expect the following exception if I deleted the item record, not if I broke the relationship.

Note: I can null the column in the SQL directly

If you are seeing an exception, include the full exceptions details (message and stack trace).

Exception message: InvalidOperationException: The association between entity types 'Item' and 'Person' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes.
Stack trace:
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.HandleConceptualNulls()
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave()
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager+<SaveChangesAsync>d__59.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.EntityFrameworkCore.DbContext+<SaveChangesAsync>d__48.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Steps to reproduce

Include a complete code listing (or project/solution) that we can run to reproduce the issue.

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

modelBuilder.Entity<Person>().HasOne<Item>().WithMany().HasForeignKey("LeftItem").OnDelete(DeleteBehavior.Restrict);
public async Task RemoveAsync(string id)
{
    var prop = _db.Attach(new Person() { ID = id}).Property("LeftItem");
    prop.CurrentValue = null;
    prop.IsModified = true;
    await _db.SaveChangesAsync();
}

Further technical details

EF Core version: 2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: win10
IDE: Visual Studio 2017 15.4

@Tarig0
Copy link
Author

Tarig0 commented Dec 12, 2017

Looks like ClientSetNull might be my guy.

@Tarig0
Copy link
Author

Tarig0 commented Dec 12, 2017

I think (based on the naming) this is an unintended restriction. Since I'm modifying the dependent record and not deleting the principle record.

@ajcvickers
Copy link
Member

Duplicate of #9703

@ajcvickers ajcvickers marked this as a duplicate of #9703 Dec 12, 2017
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants