1

enter image description here

I am trying to make TransactionId increased using Is Identity in my code. But it shows this following error :

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

I tried to put [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] syntax. But it didn't work.

public partial class TransactionHistories
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int TransactionId { get; set; }

        public DateTime? TransactionDate { get; set; }
        public long? CheckingAccountNumber { get; set; }
        public long? SavingAccountNumber { get; set; }
        [DisplayName("Transffered Amount")]
        public decimal? Amount { get; set; }
        public decimal? Transactionfee { get; set; }
        public long? TransactionTypeId { get; set; }
    }

            TransactionHistories = new TransactionHistories();
            TransactionTypes = new TransactionTypes();

            if (!ModelState.IsValid)
            {
                return Page();
            }

            SavingAccounts.Balance = SavingAccounts.Balance - Amount;
            CheckingAccounts.Balance = CheckingAccounts.Balance + Amount;

            //TransactionHistories.TransactionId = TransactionHistories.TransactionId + 1;
            TransactionHistories.TransactionDate = DateTime.Now;
            TransactionHistories.CheckingAccountNumber = CheckingAccounts.CheckingAccountNumber;
            TransactionHistories.SavingAccountNumber = SavingAccounts.SavingAccountNumber;
            TransactionHistories.Amount = Amount;
            TransactionHistories.Transactionfee = 0;
            TransactionHistories.TransactionType = "SavingToChecking";

Above is my model class and TransactionId is primary key which is the value I am trying to make it increase by 1 using Is Identity. How can I make this work? Please give me any advice and help.

  • 1
    Try adding `[key]` attribute in addition to the DatabaseGenerateOption or change the field to classname + Id (TransactionHistoriesId) – Crowcoder Dec 12 '18 at 21:39
  • I tried to put [key] attribute but still not working. Btw changing class name would matter for this issue? – Gideok Seong Dec 12 '18 at 21:45
  • Do you have access to the database and if so is it SQL? and if that is also the case do you have SSMS installed? also you have a typo in your Amount display name – Giollia Dec 12 '18 at 22:04
  • I am using SSMS and retrieved data form SSMS server into visual studio when I created the data models. – Gideok Seong Dec 12 '18 at 22:25
  • Possible duplicate of [Cannot insert explicit value for identity column in table 'table' when IDENTITY\_INSERT is set to OFF](https://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity) – Batuhan Dec 12 '18 at 22:41
  • However you are inserting, make sure you do not supply the ID. let the database generate it. – Crowcoder Dec 13 '18 at 00:23
  • I put up my code.. I am not sure whether I am supplying ID myself. – Gideok Seong Dec 13 '18 at 02:42
  • I put the values into properties in TransactionHistories except id value. – Gideok Seong Dec 13 '18 at 02:46

1 Answers1

-1

Cannot insert explicit value for identity column in table 'table' when IDENTITY_INSERT is set to OFF

SET IDENTITY_INSERT "table" ON

Batuhan
  • 729
  • 4
  • 20
  • Could you mention where I should exactly go to change that option? – Gideok Seong Dec 12 '18 at 22:49
  • You pretty much only want to do that when seeding the database. It is irresponsible to tell someone to turn identity insert on. – Crowcoder Dec 13 '18 at 00:22
  • @Gideok Seong in your sql ui ,you should write this command to command line – Batuhan Dec 13 '18 at 07:01
  • Here is Gideok want to add auto-increment column in database and you telling it is seeding database and it is irresponsible. We are focus on problems and this problem will be fix this solution 'SET IDENTİTY_INSERT "table" ON' – Batuhan Dec 13 '18 at 08:53