1

I have Pin Model

public class Pin
{
    [Key]
    [Column("pin")]
    public int Id { get; set; }

    [StringLength(50, MinimumLength = 1)]
    [Column("desc", TypeName = "varchar")]
    public string Description { get; set; }
}

I have MyDbContext

public class MyDbContext : DbContext
{
    public MyDbContext() : base("DbName")
    {
        Database.SetInitializer<MyDbContext>(new MyDbInitializer());
    }

    public virtual IDbSet<Pin> Pins { get; set; }
}

internal class MyDbInitializer : CreateDatabaseIfNotExists<MyDbContext>
{
    protected override void Seed(MyDbContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        context.SaveChanges();
        return;
    }
}

I'm using MyDbContext for adding pins

var pin = new Pin{ Description = "Test description" };
using (var context = new MyDbContext())
{
    context.Pins.Add(pin);
    context.SaveChanges();
}

After SaveChanges is done the object pin has updated Id with value 1300 for example but according to database latest Id should be 1310 and 1300 is already busy. Bug appears only in one production environment, there was no possibility to reproduce on local or staging environments Any advises about direction where to investigate is appreciated

Tim
  • 187
  • 1
  • 11
  • Try and log the results after save changes so you are sure this is happening. Log.WriteLine($"pin.Id : {pin.Id}"); var testPin = context.Pins.FirstOrDefault(x => x = pin.Id); Log.WriteLine($"testPin.Description : {testPin.Description}"); – TheGeneral Jan 14 '18 at 05:22
  • Also what DB are you using – TheGeneral Jan 14 '18 at 05:22
  • @Saruman Sorry forgot to mention I'm using MsSQL. Yes I've checked and Id is wrong - testPin is different from pin – Tim Jan 14 '18 at 05:36
  • Can you post `MyDbInitializer` code ? – Mohsen Esmailpour Jan 14 '18 at 06:38
  • which id does your pin (the one you just added) actually have when the id in the object is x? is it always higher? – DevilSuichiro Jan 14 '18 at 07:40
  • I don't think this is EF problem. Most likely some identity column corruption in the problematic database. Try [reseeding](https://stackoverflow.com/questions/21824478/reset-identity-seed-after-deleting-records-in-sql-server) it and see if that helps. – Ivan Stoev Jan 14 '18 at 12:08
  • @MohsenEsmailpour I've updated post with `MyDbInitializer` code – Tim Jan 14 '18 at 17:45

1 Answers1

0

I would add pin to the DBSET:

var pin = new Pin{ Description = "Test description" };
using (var context = new MyDbContext())
{
    context.Pins.Add(pin);//here
    context.SaveChanges();
}
Mohamoud Mohamed
  • 515
  • 7
  • 16
  • Sorry that was my typo. Just fixed initial post. I'm already using `context.Pins.Add(pin)` – Tim Jan 14 '18 at 17:15
  • No worries How many instances of dbcontext do you have spun up in production that use Pins? – Mohamoud Mohamed Jan 14 '18 at 20:21
  • It is a singleton for most cases. But there are a small amount of places where It is used with `using` as above – Tim Jan 15 '18 at 07:12
  • Ive had the same Issue before but im not sure how I created the problem, But I know for a fact that I had many DbContexts in that particular project. I only use one static Instance that gets passed around my controllers nowadays to avoid that. – Mohamoud Mohamed Jan 16 '18 at 17:49