Using a navigation property to clear a foreign key fails in 2.0.0
See original GitHub issueGiven 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:
- Created 6 years ago
- Comments:10 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for the responses.
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 inOnModelCreating()
: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 newDeleteBehavior.ClientSetNull
is perfect though, many thanks!@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.)