Reputation: 310
While trying to insert data using my ASP.NET MVC API, the id being auto generated by Entity Framework seems to be out of bounds causing problems to insert into SQL Server. The object has all the data required to be passed in and the database and already been created using a migration. I'm unsure what is causing this error. Thanks in advance for the help
An example of the id generated:
This is how I insert the data into the database
_context.Triggers.Add(trigger);
_context.SaveChanges();
The model class:
[Key]
public int Id { get; set; }
[Required]
public int TriggerType { get; set; } //1 overload, 2 power off
[Required]
public string UserId { get; set; }
[Required]
public string Name { get; set; }
Upvotes: 1
Views: 2448
Reputation: 1
If you have anything like this,
private int _id = -1;
public int Id {get => _name; set => _name = value};
Just remove it and try this,
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
This solved my problem
Upvotes: 0
Reputation: 5122
Try this:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
Upvotes: 2