1

I'm using EF6.1.3, .NET 4.6 and Web Forms. I just updated to Windows 10 and Visual Studio 2015 so it might not be the code but something completely different, but at this point I really have no idea why is this happening.

Just for this one entity, when I'm not debugging and change a value, the value gets changed, but if I try to delete that entity, the entity reverts to the old value and the code continues to execute.

If I'm debugging and go step by step trough the code, I get this exception when on FirstOrDefault:

System.InvalidOperationException: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.

The deleting code is quite simple:

protected void btnDelete_OnClick(object sender, EventArgs e)
{
    using (IContext db = new DatabaseContext(Session["cn"].ToString()))
    {
        var objId = new Guid(Request.QueryString["id"]);
        var obj = db.Objects.FirstOrDefault(x => x.Id == objId);
        db.Objects.Remove(obj);
        db.SaveChanges();
        Response.Redirect( to a different page );
    }
}

I read that I should turn MARS on, turned it on and nothing. But I do know that I don't need MARS since I'm always disposing of the context and I'm not using multiple threads so no clue as to why I get this exception.

Any ideas?

Here's the stacktrace

[InvalidOperationException: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.]
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +4263799
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +53
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +16
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +39
System.Linq.Queryable.First(IQueryable`1 source, Expression`1 predicate) +83
Community
  • 1
  • 1
GregoryHouseMD
  • 2,168
  • 1
  • 21
  • 37
  • possible duplicate of [The context cannot be used while the model is being created](http://stackoverflow.com/questions/9750115/the-context-cannot-be-used-while-the-model-is-being-created) – Jacob Aug 05 '15 at 14:25
  • Nope, not a duplicate as I tried the suggestions there (the MARS part) – GregoryHouseMD Aug 05 '15 at 14:27

0 Answers0