1

I have three small model classes and although two of them does works one is not and I can't figure out why. I found several solutions but neither of them helped me. First of all, code first approach was used in the project.

So, the main problem is that the PK in the Coupon class is not set to autoincrement value. I refer to the tables from Server Explorer and see PK's properties. Realized that other two classes PK's properties are set as Is Identity to True and Identity Increment = 1 whereas in the Coupon's PK's property they are set as Is Identity to False and Identity Increment to 0.

I think the problem is somewhere there and below you can find the small model class I am having trouble with.

public class Coupon
{
    public Coupon()
    {
        footballMatches = new List<FootballMatch>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CouponId { get; set; }
    public int UserId { get; set; }
    public virtual List<FootballMatch> footballMatches { get; set; }
}

Ask me more if you need further information.

Thanks !

6
  • Not an answer to your question, but is there any reason you wouldn't use GUIDs instead of integers? msdn.microsoft.com/en-us/library/system.guid(v=vs.110).aspx Commented Feb 10, 2016 at 12:21
  • Most probably I am new to c#, nothing special. Commented Feb 10, 2016 at 12:22
  • using int is not to uncommon Commented Feb 10, 2016 at 12:29
  • Okay I will try to get used to them thanks. Lets focus on my question now haha :) Commented Feb 10, 2016 at 12:36
  • 2
    Well, if the PK for Coupon is set to Is Identity=false in the database, then it is NOT an identity column and therefore will not auto-increment...... you need to delete that column and re-add it again, this time with the identity spec set to true Commented Feb 10, 2016 at 12:53

1 Answer 1

1

Not expected that, but I found the solution in such a silly way. The solution to my problem was just re-adding the key. At first, I did not agree with marc_s because I also tried re-adding before. Apparently, I did in a wrong way when I firstly tried re-adding.

What I made wrong is converting int to string then converting back to int while I did not touch the name of the instance, which is CouponId. That did not work. However, and sadly, I also had to change the name of the instance as well to make it work. I changed the line public int CouponId to public string CouponName then convert back to the actual name. Now it shows the expected behaviour.

Briefly, the simple solution is to change the key both variable and name and alter them to their actual names again. That simply.

By the way, while doing them please do not forget to update your migrations between each step. So the workflow is like Change->Update->Change Back->Update.

Hope that can help others who went through the same trouble.

Sign up to request clarification or add additional context in comments.

1 Comment

More explanation of what has happened here - stackoverflow.com/a/18917348/150342

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.