The property 'Id' on entity type 'EntityType' has a temporary value while attempting to change the entity's state

See original GitHub issue

image

Whilst making an insertion in to the db of an entity I’ve recently started receiving this and i can’t see why …

The property ‘Id’ on entity type ‘Page’ has a temporary value while attempting to change the entity’s state to ‘Unchanged’. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.

The entity definition begins with …

    [Table("Pages", Schema = "CMS")]
    public class Page
    {
        [Key]
        public int Id { get; set; }
        ...

… the resulting EF Core migration generates this …

migrationBuilder.CreateTable(
    name: "Pages",
    schema: "CMS",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),

Which in turn looks like this when run on the DB server …

image

… I can’t see any reason why the primary key for that table isn’t entirely managed by EF / SQL here so when I do osmething like …

var newPage = context.Add(new Page { … });

… and leave the Id on it’s default I would expect the newPage variable to be populated with a DB generated primary key value.

What am I missing here?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:27 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
TehWardycommented, Mar 7, 2020

That right there is the source of most of my frustrations with all .Net stack based solutions. It’s like there is no official guideline about what is compatible with what and if you find a scenario that sits in the gap between two frameworks the answer is usually cryptic or vague.

The net result is often that simply explaining the issue is non trivial and even if you agree that my issue is a genuine “problem in the technology” I still have the issue of trying to convince someone on one team or another that they should own the issue.

It would really help if teams on your end collaborated more to discuss feature sets and released guidelines and version “sets” of the various pieces that we are (as consumers) trying to bolt together. e.g OData + EF seems like a no brainer and yet rarely is.

0reactions
TehWardycommented, Apr 24, 2020

Try updating the entities to something like this (although I admit I can’t see how this would affect the outcome) …

public class T
{
   [Key]
    public int Id { get; set; }
    public string ScalarX { get; set; }
    public ICollection<Foo> ChildFoos { get; set; }
}

public class Foo
{
    [Key]
    public int Id { get; set; }
    [ForeignKey("T")]
    public int TId {  get; set; }
    public string ScalarY { get; set; }
    public virtual T T { get; set; }
}

… or maybe the issue is that the primary keys are all guids? I have noticed some odd stuff with Guid keys, despite using the DatabaseGenerated attribute it doesn’t always set the system up to generate them on the db.

I wonder if it’s caused by the parent PK being computed and not given to the child entity set before the add. I’ll check through the history of my code to see if that could be related to my problem. Or maybe some mix of db generated guids and code generated guids i’m missing in my code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Temporary Value Error During Entity Framework Core Modify
System.InvalidOperationException : 'The property 'Id' on entity type 'UserData' has a temporary value while attempting to change the entity's ...
Read more >
The property on entity type has a temporary value while ...
Coding example for the question The property on entity type has a temporary value while attempting to change the entity's state to 'Deleted'....
Read more >
The property 'Id' on entity type 'Customer' has a temporary ...
The property 'Id' on entity type 'Customer' has a temporary value while attempting to change the entity's state to 'Unchanged'.
Read more >
How do I prevent a temporary value being assigned when ...
The property 'Id' on entity type 'LoadTable' has a temporary value while attempting to change the entity's state to 'Modified'.
Read more >
Identity Resolution - EF Core
Resolving multiple entity instances into a single instance using primary key values.
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