question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using a navigation property to clear a foreign key fails in 2.0.0

See original GitHub issue

Given the following two (simplified) models, configured with the foreign key as DeleteBehavior.Restrict:

public class Asset
{
    public int Id { get; set; }
}

public class Product
{
    public int Id { get; set; }

    public int? DownloadAssetId { get; set; }
    public Asset DownloadAsset { get; set; }
}

Trying to clear the asset with product.DownloadAsset = null; (after loading the data with .Include(x => x.DownloadAsset)) results in the following error:

The association between entity types 'Asset' and 'Product' 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.

Clearing with product.DownloadAssetId = null; works as expected.

Obviously I don’t want any entities to be deleted in this case; the only action I expect to occur is for the specified DownloadAssetId to be set to null.

It’s kind of difficult for me to test it now, but I’m pretty sure this worked as I expected in 1.1.x.

Further technical details

EF Core version: 2.0.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio 2017 15.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
svalliscommented, Aug 19, 2017

Thanks for the responses.

Question: why were you explicitly configuring Restrict in your 1.1.x code?

Bear in mind this code base has been around since pre 1.0.0. Some of the foreign keys were being created in my migrations with ReferentialAction.Cascade, which I didn’t want. I added the following in OnModelCreating():

// make referential delete behaviour restrict instead of cascade for everything
foreach (var relationship in builder.Model.GetEntityTypes().SelectMany(x => x.GetForeignKeys()))
{
    relationship.DeleteBehavior = DeleteBehavior.Restrict;
}

Commenting that out in 2.0.0 and generating a new migration is still trying to create ReferentialAction.Cascade relationships in places that I don’t want. Switching to the new DeleteBehavior.ClientSetNull is perfect though, many thanks!

1reaction
ajcvickerscommented, Aug 19, 2017

@svallis Thanks for the information. Glad it is working for you now. (FYI: relationships get cascade delete by convention when the FK is not nullable.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using a navigation property to clear a foreign key fails ...
The association between entity types 'Asset' and 'Product' has been severed but the foreign key for this relationship cannot be set to null....
Read more >
Repository insert with navigation property throws ...
Repository insert with navigation property throws 'FOREIGN KEY constraint failed' · 1. You insert a Entity2 without any reference to an Entity1 ....
Read more >
Breaking changes included in EF Core 3.x
Not using store-generated keys. Setting navigation properties to form relationships instead of setting foreign key values.
Read more >
ef core foreign key
ef core foreign keyForeign and principal keys in relationships - EF Core. Navigation properties without foreign keys #20744 - GitHub.
Read more >
ef core foreign key
Add Foreign key in existing database tables using EF Core . ... -verbose throwing the below error To change the IDENTITY property of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found