0

I am using a a simple coding to add record in table using Entity Framework i.e

Transaction t = new Transaction();
t.CustomerID = 1; 
t.Date = DateTime.Now; 
t.Balance = 400;
t.Total = 500;
t.Paid = 100;

db.Transactions.Add(t);
db.SaveChanges();

But after some entering some record the primary key is unexpectedly change

enter image description here

Please suggest me some suggestion Thanks.

Edited..

Table Detail

CREATE TABLE [hassan].[Transaction]
(
    [TransactionId] [int] IDENTITY(1,1) NOT NULL,
    [Date] [datetime] NULL,
    [TransactionType] [nvarchar](20) NULL,
    [TransactionTypeID] [int] NULL,
    [Total] [numeric](18, 0) NULL,
    [Paid] [numeric](18, 0) NULL,
    [Balance] [numeric](18, 0) NULL,
    [Note] [nvarchar](50) NULL,
    [CustomerID] [int] NULL,
    [UserID] [int] NULL,
    [BranchID] [int] NULL,
    [Referance] [nvarchar](20) NULL,
    [SupplierID] [int] NULL,

    CONSTRAINT [PK_Transaction_1] 
     PRIMARY KEY CLUSTERED ([TransactionId] ASC)
          WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Umar Abbas
  • 4,041
  • 2
  • 23
  • 21
  • Have you added in your table declaration the `TransactionId` to be an IDENTITY COLUMN (1,1)? – Christos Feb 02 '15 at 17:09
  • Looks like you (or something) deleted a bunch of records. Are you basically saying calling the above code 11 times yields the results in the screenshot? – Ciaran Feb 02 '15 at 17:09
  • @Christos yes i check it is IDENTITY COLUMN (1,1) – Umar Abbas Feb 02 '15 at 17:11
  • @UmarAbbas then that Ciaran claims must be the reason. – Christos Feb 02 '15 at 17:12
  • @Ciaran no one delete the data. yes only running this code 11 time. i think their is not problem in code but problem in database – Umar Abbas Feb 02 '15 at 17:13
  • This happens to me sometimes. I also have set the `IDENTITY COLUMN (1,1)` and never deleted records from the table. – Nikolay Kostov Feb 02 '15 at 17:14
  • can you show the actual table schema then so we can see if it's a db table create issue or not..if that happens sometimes, sounds like you might need to change / get away from using Entity to do your Updates – MethodMan Feb 02 '15 at 17:14
  • 5
    Did SQL Server restart? Known issue, [closed as by design](https://connect.microsoft.com/SQLServer/feedback/details/739013/failover-or-restart-results-in-reseed-of-identity). Next question: do you care about missing values? If so, WHY? [Please read this](http://sqlity.net/en/792/the-gap-in-the-identity-value-sequence/). If it's really important, [you can use TF 272](http://www.sqlservercentral.com/Forums/Topic1491658-2799-1.aspx). Otherwise, just don't restart SQL Server all the time. – Aaron Bertrand Feb 02 '15 at 17:18
  • @AaronBertrand Yes i shutdown the system and next day when i start entering some data it is Primary key jumps to 1000 as u can see in picture i attach. – Umar Abbas Feb 02 '15 at 17:24
  • 2
    Yep, same issue. So you can do one of two things: use the trace flag locally to make you feel like you are immune from this problem (but you should not run that trace flag in production IMHO since it will have serious performance implications in a high-volume system). Or you can stop caring about gaps. – Aaron Bertrand Feb 02 '15 at 17:30

0 Answers0