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 !
Couponis set toIs Identity=falsein 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 totrue