In Entity Framework id value doesn't get incremented by 1, but by 1000? [duplicate] Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Huge gap in increment value of identity columnGet int value from enum in C#Get property value from string using reflection in C#SqlException from Entity Framework - New transaction is not allowed because there are other threads running in the sessionHow can I get Id of inserted entity in Entity framework?Fastest Way of Inserting in Entity FrameworkEntity Framework TimeoutsEntity Framework Provider type could not be loaded?Entity Framework 5 Updating a RecordHow to update record using Entity Framework 6?Updating Memory Optimized Table

Is there any hidden 'W' sound after 'comment' in : Comment est-elle?

How would this chord from "Rocket Man" be analyzed?

Is a 5 watt UHF/VHF handheld considered QRP?

What is this word supposed to be?

My admission is revoked after accepting the admission offer

How would I use different systems of magic when they are capable of the same effects?

Arriving in Atlanta after US Preclearance in Dublin. Will I go through TSA security in Atlanta to transfer to a connecting flight?

Function to calculate red-edgeNDVI in Google Earth Engine

Co-worker works way more than he should

Would reducing the reference voltage of an ADC have any effect on accuracy?

Is Electric Central Heating worth it if using Solar Panels?

What's the difference between using dependency injection with a container and using a service locator?

What is it called when you ride around on your front wheel?

What's parked in Mil Moscow helicopter plant?

Passing args from the bash script to the function in the script

Has a Nobel Peace laureate ever been accused of war crimes?

Trumpet valves, lengths, and pitch

Is accepting an invalid credit card number a security issue?

A Paper Record is What I Hamper

What to do with someone that cheated their way through university and a PhD program?

Justification for leaving new position after a short time

Are all CP/M-80 implementations binary compatible?

Israeli soda type drink

The art of proof summarizing. Are there known rules, or is it a purely common sense matter?



In Entity Framework id value doesn't get incremented by 1, but by 1000? [duplicate]



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Huge gap in increment value of identity columnGet int value from enum in C#Get property value from string using reflection in C#SqlException from Entity Framework - New transaction is not allowed because there are other threads running in the sessionHow can I get Id of inserted entity in Entity framework?Fastest Way of Inserting in Entity FrameworkEntity Framework TimeoutsEntity Framework Provider type could not be loaded?Entity Framework 5 Updating a RecordHow to update record using Entity Framework 6?Updating Memory Optimized Table



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0
















This question already has an answer here:



  • Huge gap in increment value of identity column

    2 answers



In the following table, you can see that billid column is increased by 1000 at 10th row.



enter image description here



The code to generate new bill is as follows:



private void NewBill()

var bill = new Bill

BillDate = DateTime.Now,
InUse = true,
Paid = false,
TableId = _tableId,
UserId = frmLogIn.LoggedInUserId
;

using (var context = new Context())

context.Bills.Add(bill);
context.SaveChanges();




Why it does not get incremented by just 1 ?



I am using EF6 with SQL Server 2017 Express edition.










share|improve this question















marked as duplicate by CodingYoshi, Lankymart, marc_s c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 9 at 9:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 4





    Why do you care? It has nothing to do with EF. It is the db server that does this and you cannot control it.

    – CodingYoshi
    Mar 9 at 6:15












  • Did you have a look at your table definition??

    – Gagan Deep
    Mar 9 at 7:03











  • This has nothing to do with EF - it's a feature in SQL Server - read more about here on dba.stackexchange.com

    – marc_s
    Mar 9 at 9:27

















0
















This question already has an answer here:



  • Huge gap in increment value of identity column

    2 answers



In the following table, you can see that billid column is increased by 1000 at 10th row.



enter image description here



The code to generate new bill is as follows:



private void NewBill()

var bill = new Bill

BillDate = DateTime.Now,
InUse = true,
Paid = false,
TableId = _tableId,
UserId = frmLogIn.LoggedInUserId
;

using (var context = new Context())

context.Bills.Add(bill);
context.SaveChanges();




Why it does not get incremented by just 1 ?



I am using EF6 with SQL Server 2017 Express edition.










share|improve this question















marked as duplicate by CodingYoshi, Lankymart, marc_s c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 9 at 9:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 4





    Why do you care? It has nothing to do with EF. It is the db server that does this and you cannot control it.

    – CodingYoshi
    Mar 9 at 6:15












  • Did you have a look at your table definition??

    – Gagan Deep
    Mar 9 at 7:03











  • This has nothing to do with EF - it's a feature in SQL Server - read more about here on dba.stackexchange.com

    – marc_s
    Mar 9 at 9:27













0












0








0









This question already has an answer here:



  • Huge gap in increment value of identity column

    2 answers



In the following table, you can see that billid column is increased by 1000 at 10th row.



enter image description here



The code to generate new bill is as follows:



private void NewBill()

var bill = new Bill

BillDate = DateTime.Now,
InUse = true,
Paid = false,
TableId = _tableId,
UserId = frmLogIn.LoggedInUserId
;

using (var context = new Context())

context.Bills.Add(bill);
context.SaveChanges();




Why it does not get incremented by just 1 ?



I am using EF6 with SQL Server 2017 Express edition.










share|improve this question

















This question already has an answer here:



  • Huge gap in increment value of identity column

    2 answers



In the following table, you can see that billid column is increased by 1000 at 10th row.



enter image description here



The code to generate new bill is as follows:



private void NewBill()

var bill = new Bill

BillDate = DateTime.Now,
InUse = true,
Paid = false,
TableId = _tableId,
UserId = frmLogIn.LoggedInUserId
;

using (var context = new Context())

context.Bills.Add(bill);
context.SaveChanges();




Why it does not get incremented by just 1 ?



I am using EF6 with SQL Server 2017 Express edition.





This question already has an answer here:



  • Huge gap in increment value of identity column

    2 answers







c# entity-framework-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 9:21









marc_s

586k13011281273




586k13011281273










asked Mar 9 at 6:06









Walchand WarikWalchand Warik

11




11




marked as duplicate by CodingYoshi, Lankymart, marc_s c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 9 at 9:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by CodingYoshi, Lankymart, marc_s c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 9 at 9:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 4





    Why do you care? It has nothing to do with EF. It is the db server that does this and you cannot control it.

    – CodingYoshi
    Mar 9 at 6:15












  • Did you have a look at your table definition??

    – Gagan Deep
    Mar 9 at 7:03











  • This has nothing to do with EF - it's a feature in SQL Server - read more about here on dba.stackexchange.com

    – marc_s
    Mar 9 at 9:27












  • 4





    Why do you care? It has nothing to do with EF. It is the db server that does this and you cannot control it.

    – CodingYoshi
    Mar 9 at 6:15












  • Did you have a look at your table definition??

    – Gagan Deep
    Mar 9 at 7:03











  • This has nothing to do with EF - it's a feature in SQL Server - read more about here on dba.stackexchange.com

    – marc_s
    Mar 9 at 9:27







4




4





Why do you care? It has nothing to do with EF. It is the db server that does this and you cannot control it.

– CodingYoshi
Mar 9 at 6:15






Why do you care? It has nothing to do with EF. It is the db server that does this and you cannot control it.

– CodingYoshi
Mar 9 at 6:15














Did you have a look at your table definition??

– Gagan Deep
Mar 9 at 7:03





Did you have a look at your table definition??

– Gagan Deep
Mar 9 at 7:03













This has nothing to do with EF - it's a feature in SQL Server - read more about here on dba.stackexchange.com

– marc_s
Mar 9 at 9:27





This has nothing to do with EF - it's a feature in SQL Server - read more about here on dba.stackexchange.com

– marc_s
Mar 9 at 9:27












1 Answer
1






active

oldest

votes


















0














It could be due to various reasons as explained here as pointed out by @CodingYoshi. To avoid such mishaps, you could opt to generate the BillId that you can almost be sure to be sequential by fetching the last BillId and incremeting it by 1.



You could write a function like below



public int NextId()

int lastId = 0;

var billsFromStore = _context.Bills;

if (billsFromStore.Any())

var idsFromStore = _context.Bills.Select(x => x.BillId);

lastId = idsFromStore.Max();


return ++lastId;



you may want to use a lock statement to prevent different thread incrementing the value






share|improve this answer

























  • And then get all sorts of race conditions as soon as the code runs in production.

    – huysentruitw
    Mar 9 at 9:41











  • Why would you do this, to gain what? Also imagine this happens and it will: one thread gets next id of 21, and releases the lock but has more work to do before inserting record into table. Now another thread asks for id, it will also get 21. Boom!

    – CodingYoshi
    Mar 9 at 13:06

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














It could be due to various reasons as explained here as pointed out by @CodingYoshi. To avoid such mishaps, you could opt to generate the BillId that you can almost be sure to be sequential by fetching the last BillId and incremeting it by 1.



You could write a function like below



public int NextId()

int lastId = 0;

var billsFromStore = _context.Bills;

if (billsFromStore.Any())

var idsFromStore = _context.Bills.Select(x => x.BillId);

lastId = idsFromStore.Max();


return ++lastId;



you may want to use a lock statement to prevent different thread incrementing the value






share|improve this answer

























  • And then get all sorts of race conditions as soon as the code runs in production.

    – huysentruitw
    Mar 9 at 9:41











  • Why would you do this, to gain what? Also imagine this happens and it will: one thread gets next id of 21, and releases the lock but has more work to do before inserting record into table. Now another thread asks for id, it will also get 21. Boom!

    – CodingYoshi
    Mar 9 at 13:06















0














It could be due to various reasons as explained here as pointed out by @CodingYoshi. To avoid such mishaps, you could opt to generate the BillId that you can almost be sure to be sequential by fetching the last BillId and incremeting it by 1.



You could write a function like below



public int NextId()

int lastId = 0;

var billsFromStore = _context.Bills;

if (billsFromStore.Any())

var idsFromStore = _context.Bills.Select(x => x.BillId);

lastId = idsFromStore.Max();


return ++lastId;



you may want to use a lock statement to prevent different thread incrementing the value






share|improve this answer

























  • And then get all sorts of race conditions as soon as the code runs in production.

    – huysentruitw
    Mar 9 at 9:41











  • Why would you do this, to gain what? Also imagine this happens and it will: one thread gets next id of 21, and releases the lock but has more work to do before inserting record into table. Now another thread asks for id, it will also get 21. Boom!

    – CodingYoshi
    Mar 9 at 13:06













0












0








0







It could be due to various reasons as explained here as pointed out by @CodingYoshi. To avoid such mishaps, you could opt to generate the BillId that you can almost be sure to be sequential by fetching the last BillId and incremeting it by 1.



You could write a function like below



public int NextId()

int lastId = 0;

var billsFromStore = _context.Bills;

if (billsFromStore.Any())

var idsFromStore = _context.Bills.Select(x => x.BillId);

lastId = idsFromStore.Max();


return ++lastId;



you may want to use a lock statement to prevent different thread incrementing the value






share|improve this answer















It could be due to various reasons as explained here as pointed out by @CodingYoshi. To avoid such mishaps, you could opt to generate the BillId that you can almost be sure to be sequential by fetching the last BillId and incremeting it by 1.



You could write a function like below



public int NextId()

int lastId = 0;

var billsFromStore = _context.Bills;

if (billsFromStore.Any())

var idsFromStore = _context.Bills.Select(x => x.BillId);

lastId = idsFromStore.Max();


return ++lastId;



you may want to use a lock statement to prevent different thread incrementing the value







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 at 7:20

























answered Mar 9 at 7:14









xariezxariez

9211




9211












  • And then get all sorts of race conditions as soon as the code runs in production.

    – huysentruitw
    Mar 9 at 9:41











  • Why would you do this, to gain what? Also imagine this happens and it will: one thread gets next id of 21, and releases the lock but has more work to do before inserting record into table. Now another thread asks for id, it will also get 21. Boom!

    – CodingYoshi
    Mar 9 at 13:06

















  • And then get all sorts of race conditions as soon as the code runs in production.

    – huysentruitw
    Mar 9 at 9:41











  • Why would you do this, to gain what? Also imagine this happens and it will: one thread gets next id of 21, and releases the lock but has more work to do before inserting record into table. Now another thread asks for id, it will also get 21. Boom!

    – CodingYoshi
    Mar 9 at 13:06
















And then get all sorts of race conditions as soon as the code runs in production.

– huysentruitw
Mar 9 at 9:41





And then get all sorts of race conditions as soon as the code runs in production.

– huysentruitw
Mar 9 at 9:41













Why would you do this, to gain what? Also imagine this happens and it will: one thread gets next id of 21, and releases the lock but has more work to do before inserting record into table. Now another thread asks for id, it will also get 21. Boom!

– CodingYoshi
Mar 9 at 13:06





Why would you do this, to gain what? Also imagine this happens and it will: one thread gets next id of 21, and releases the lock but has more work to do before inserting record into table. Now another thread asks for id, it will also get 21. Boom!

– CodingYoshi
Mar 9 at 13:06





Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved