none
Confused from ObjectStateManager behavior RRS feed

  • Question

  • using(GenericRepository rep = GenericRepository.GetInstance())
    
    {
    
     DailyItem di = rep
       .GetQuery<FavDay>(fd => fd.Id ==id)
       .Include2("DailyItems")
       .AsNoTracking()
       .Single()
       .DailyItems
       .AsQueryable()
       .AsNoTracking()
       .Single(dii => dii.IsMaster);
    
     ((IObjectContextAdapter) rep.DbContextInstance)
       .ObjectContext
       .ObjectStateManager
       .GetObjectStateEntry(di);
    //throws System.InvalidOperationException: 
    //The ObjectStateManager 
    //does not contain an ObjectStateEntry 
    //with a reference to an object of type 
    //'Grafton.eAttendance.Service.Model.DailyItem'.
    
     rep.Attach<DailyItem>(di)
    //throws System.InvalidOperationException:
    //An object with the same key already exists 
    //in the ObjectStateManager. 
    //The ObjectStateManager cannot track multiple objects 
    //with the same key.
    
    }
    


    Anyone knows what does it mean, please?

    (my environment: EF Code First 4.2 with AutoDetectChangesEnabled = false and ProxyCreationEnabled = false).

     

    Thanks, Petr

     

    Wednesday, February 1, 2012 2:50 PM

Answers

  • Hi Petr601,

    Welcome to MSDN Forum.

    I create a simple project to repro the issue, but I didn't received the exception after attach the entity.

    Here's my test code,

     class Program
        {
            static void Main(string[] args)
            {
                testContext context = new testContext();
                TestEntity t = context.entities.AsNoTracking().Single();
                Console.WriteLine(context.Entry(t).State);
                context.entities.Attach(t);
                Console.WriteLine(context.Entry(t).State);            
                Console.Read();
            }
        }
    
        class TestEntity
        {
            public int id { get; set; }
            public string name { get; set; }
        }
    
        class testContext : DbContext
        {
            public testContext()
            {
                this.Configuration.ProxyCreationEnabled = false;
                this.Configuration.AutoDetectChangesEnabled = false;
            }
            public DbSet<TestEntity> entities { get; set; }
        }
    

    The two state I print is 'Detached' and 'Unchanged'.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Friday, February 3, 2012 6:27 AM
    Moderator

All replies

  • Hi Petr601,

    Welcome to MSDN Forum.

    I create a simple project to repro the issue, but I didn't received the exception after attach the entity.

    Here's my test code,

     class Program
        {
            static void Main(string[] args)
            {
                testContext context = new testContext();
                TestEntity t = context.entities.AsNoTracking().Single();
                Console.WriteLine(context.Entry(t).State);
                context.entities.Attach(t);
                Console.WriteLine(context.Entry(t).State);            
                Console.Read();
            }
        }
    
        class TestEntity
        {
            public int id { get; set; }
            public string name { get; set; }
        }
    
        class testContext : DbContext
        {
            public testContext()
            {
                this.Configuration.ProxyCreationEnabled = false;
                this.Configuration.AutoDetectChangesEnabled = false;
            }
            public DbSet<TestEntity> entities { get; set; }
        }
    

    The two state I print is 'Detached' and 'Unchanged'.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Friday, February 3, 2012 6:27 AM
    Moderator
  • Hi Petr601,

    Have you solved the issue? I look forward to hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, February 7, 2012 2:34 AM
    Moderator