Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Error in Update child record EF Core 3.0 The association between entities has been severed but the relationship is either marked as 'Required'

$
0
0

I'm using EF Core version: 3.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer

I have two tables: PROFILO (master) and PROFILOTITOLODISTUDIO (details) this is the model:

PROFILO MODEL

publicpartialclassProfilo{publicProfilo(){ProfiloTitoloDiStudio=newList<ProfiloTitoloDiStudio>();}[Key]publicGuidIdProfilo{get;set;}[Required][StringLength(256)]publicstringNome{get;set;}[StringLength(256)]publicstringCognome{get;set;}[InverseProperty("IdProfiloNavigation")]publicvirtualIList<ProfiloTitoloDiStudio>ProfiloTitoloDiStudio{get;set;}}

PROFILOTITOLODISTUDIO MODEL

publicpartialclassProfiloTitoloDiStudio{[Key]publicintIdProfiloTitoloDiStudio{get;set;}publicGuidIdProfilo{get;set;}[Required][StringLength(256)]publicstringDenominazioneCorsoStudio{get;set;}[ForeignKey(nameof(IdProfilo))][InverseProperty(nameof(Profilo.ProfiloTitoloDiStudio))]publicvirtualProfiloIdProfiloNavigation{get;set;}}

The IdProfilo foreign key in the PROFILOTITOLODISTUDIO tablemust be NOT NULL

In the controller's Edit action I receive the object with the fields bounded by the view as an input, even if I don't change any value, thesavechanges () always returns the error:

InvalidOperationException: The association between entities 'Profilo' and 'ProfiloTitoloDiStudio' with the key value '{IdProfilo: 45c42779-39b6-4047-91d1-12253a79b7f6}' has been severed but the relationship is either marked as 'Required' or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship is severed, then setup the relationship to use cascade deletes.

this is the save code:

publicasyncTask<IActionResult>Edit(Profilo profiloinput){var _profiloDB =await _dbcontext.Profilo.Include(p => p.ProfiloTitoloDiStudio).SingleOrDefaultAsync(p => p.IdProfilo== profiloinput.IdProfilo);

            _profiloDB.Nome= profiloinput.Nome;
            _profiloDB.Cognome= profiloinput.Cognome;


            _profiloDB.ProfiloTitoloDiStudio= profiloinput.ProfiloTitoloDiStudio;var x =await _dbcontext.SaveChangesAsync();}

I am not breaking any relationship between PROFILE and PROFILOTITOLODISTUDIO I am simply trying to make an update on the child table, the IDPROFILO foreign key in the profile object.ProfiloTitoloDiStudio is perfectly enhanced !!!


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>