Honestly, your approach of attempting to internally track id's within your app is going to be problematic. For one thing, if an insert error occurs, an id can be skipped (and throw off what your app thinks the next id will be).

If I were you, I would not try to track the id in my app, I would find an approach that inserts the record and then returns the new id to me.

One way to do this is to write a sproc that returns the Id as an OUT parameter.

Another way is to do this within Entity Framework.