Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasKey and ValueGeneratedOnAdd have a declaration order dependency #5776

Closed
alexandru-calinoiu opened this issue Jun 15, 2016 · 1 comment
Closed
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@alexandru-calinoiu
Copy link

Steps to reproduce

PropertyClassEntity

    public class PropertyClassEntity
    {
        public override PropertyClassEntityTypeEnum Id { get; set; }
    }

PropertyEntity.cs

    public sealed class PropertyEntity : Entity
    {
        public PropertyClassEntity PropertyClass { get; set; }
    }

PropertyClassEntityTypeEnum

    public enum PropertyClassEntityTypeEnum : byte
    {
        A = 1,
        B,
        C
    }

CommonMapping (works)

            builder.Property(t => t.Id).ValueGeneratedOnAdd();
            builder.HasKey(t => t.Id);

CommonMapping (does not work)

            builder.HasKey(t => t.Id);
            builder.Property(t => t.Id).ValueGeneratedOnAdd();

The issue

When trying to save the PropertyEntity it will throw the following error:

Result Message: The property 'Id' on entity type 'PropertyClassEntity' 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.

Further technical details

EF Core version: 1.0.0-rc2-final
Operating system: Win10
Visual Studio version: VS 2015

@ajcvickers
Copy link
Contributor

Notes for triage: I wasn't able to reproduce the exact exception, probably because there is a lot of missing information in the repro code, but I did find the likely root cause. When ValueGeneratedOnAdd is set a check is made for whether the property is part of a key. If it is, then it gets the RequiresValueGenerator flag set so that the stack will know to generate temporary key values. For non-key properties this does not happen. But if something is made a key, then there is no code that goes back and checks whether any properties have OnAdd set, so in this case the temporary keys are not created.

I think there may be other issues around byte and/or enum keys, so I am going to do further investigation.

@rowanmiller rowanmiller added this to the 1.1.0 milestone Jun 15, 2016
@rowanmiller rowanmiller removed the pri0 label Jul 6, 2016
AndriySvyryd added a commit that referenced this issue Aug 3, 2016
…roperty

Change the default of RequiresValueGenerator to simplify KeyConvention

Fixes #5776
AndriySvyryd added a commit that referenced this issue Aug 4, 2016
…roperty

Change the default of RequiresValueGenerator to simplify KeyConvention

Fixes #5776
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 4, 2016
@AndriySvyryd AndriySvyryd removed their assignment Aug 4, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants