Search code examples
c#sqlentity-frameworkasp.net-core-2.2

SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF


I am creating a website for expense tracking and when I send a new POST request calling AddNew method to add a new Entry I get this error:

SqlException: Cannot insert explicit value for identity column in table 'Accounts' when IDENTITY_INSERT is set to OFF. Cannot insert explicit value for identity column in table 'Categories' when IDENTITY_INSERT is set to OFF.

public class Entry
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Description { get; set; }
    public decimal MoneyAmount { get; set; }
    public virtual Account Account { get; set; }
    public virtual Category Category { get; set; }
    public string CreatedTimestamp { get; set; }
}

public class Account
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual User User { get; set; }
    //public byte[] BankApi { get; set; }
    public decimal MoneyAmount { get; set; }
    public string CreatedTimestamp { get; set; }
    public virtual ICollection<Entry> Entries { get; set; }
}

public class Category
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual EntryType EntryType { get; set; }
    public virtual ICollection<Entry> Entries { get; set; }
}

public interface IEntryService
{
    void AddNew(Entry entry);
}

public class EntryService : IEntryService
{
    private DataContext _context;

    public EntryService(DataContext context)
    {
        _context = context;
    }

    public void AddNew(Entry entry)
    {
        entry.CreatedTimestamp = DateTime.Now.ToString();

        _context.Entries.Add(entry);
        _context.SaveChanges();
    }
}

Here is my postman request:

Postman request

Account with ID == 1, and Category with ID == 1 both exist already in the respective databases.

Also, when I am inserting values to Account database, whose method looks exactly the same as AddNew for entries, it works without a problem.

Does anyone know what is this about?


Solution

  • I just changed Entry model from

    public class Entry
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Description { get; set; }
        public decimal MoneyAmount { get; set; }
        public virtual Account Account { get; set; }
        public virtual Category Category { get; set; }
        public string CreatedTimestamp { get; set; }
    }
    

    To this:

     public class Entry
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int Id { get; set; }
            public string Description { get; set; }
            public decimal MoneyAmount { get; set; }
            public int AccountId { get; set; }
            public int CategoryId { get; set; }
            public string CreatedTimestamp { get; set; }
        }
    

    And everything seems to work fine now. C# does its magic and picks AccountId and CategoryId as foreign keys.