2021年5月3日星期一

EF Core Entity Post Error with entity that has other entities as properties

Situation:

  • Code First C# Azure Function for Posting a new Activity
  • Activity has Id, Helper and Round. Last two are also entities, ie FKs
  • Whilst I can Post new Helpers and Rounds, when posting a new Activity using existing Helper and Round... I'm Getting this error:
Executed 'ActivitysPost' (Failed, Id=a2e8a556-7b1b-4d0d-995c-65b9c494c802, Duration=26938ms)  [2021-05-03T05:04:29.141Z] System.Private.CoreLib: Exception while executing function: ActivitysPost.   Microsoft.EntityFrameworkCore.Relational: An error occurred while updating the entries.   See the inner exception for details.   Core .Net SqlClient Data Provider:   Cannot insert explicit value for identity column in table 'Helpers'   ... when IDENTITY_INSERT is set to OFF.  

Error occurs on 2nd line:

            _context.Add(activity);              await _context.SaveChangesAsync();  

Using EF Core 2.2.3

NB: The following does work (ie Submitting new Helper and Round):

curl --header "Content-Type: application/json" --request POST --data "{'Name':'Recording','Quantity':1,'Round':{'No':1,'Description':'AVSL'},'Helper':{'Name':'FreedyFeelgood','Description':'BlaBlah'}}" http://localhost:7071/api/activitys/  

Result returned:

{"id":6,"name":"Recording","quantity":1,"helper":{"id":13,"name":"FreedyFeelgood","description":"BlaBlah"},"round":{"id":7,"no":1,"description":"AVSL"}}    
https://stackoverflow.com/questions/67363783/ef-core-entity-post-error-with-entity-that-has-other-entities-as-properties May 03, 2021 at 01:15PM

没有评论:

发表评论