简体   繁体   中英

NullReferenceException when calling DbContext.Entry

I am facing this strange behavior that sometimes, when i try to Reload a Entry with:

DbEntityEntry<BatchServer> ent = d.Entry(jobServer);
//the ex is thrown at d.Entry
ent.Reload();

EF throws a NullReferenceException Exception. But neither jobServer nor d ( Variable of my Context ) is null and if we take a look into the StackTrace:

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Objects.EntityEntry.DetectChangesInProperty(Int32 ordinal, Boolean detectOnlyComplexProperties, Boolean detectOnly)
at System.Data.Objects.EntityEntry.DetectChangesInProperties(Boolean detectOnlyComplexProperties)
at System.Data.Objects.ObjectStateManager.DetectChangesInScalarAndComplexProperties(IList`1 entries)
at System.Data.Objects.ObjectStateManager.DetectChanges()
at System.Data.Objects.ObjectContext.DetectChanges()
at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)
at System.Data.Entity.Internal.InternalContext.GetStateEntry(Object entity)
at System.Data.Entity.Internal.InternalEntityEntry..ctor(InternalContext internalContext, Object entity)
at System.Data.Entity.DbContext.Entry[TEntity](TEntity entity)
at IEADPC.BatchRemoting.Monitor.ViewModel.MonitorViewModel.<>c__DisplayClass40.<>c__DisplayClass42.<RefreshAll>b__3d(BatchRemotingContexA first chance exception of type 'System.NullReferenceException' occurred in IEADPC.BatchRemoting.Monitor.dll

we see that there is a exception inside of EF. This bug is to 100% reproducible but only after a lot work in the program. So is that a known bug if EF or what am i doing wrong?

Greetings

Update:

Just befor i call this i get all server from the Context:

    DataAccessObject.ClientDataAccess.WorkOnDatabase(d =>
    {
        var colzwei = new ObservableCollection<BatchServer>(d.BatchServers.ToList());
        foreach (var jobServer in colzwei)
        {
            DbEntityEntry<BatchServer> ent;                                 
            ent = d.Entry(jobServer);
            ent.Reload();
            ...
        }
    }

    public void WorkOnDatabase(Action<BatchRemotingContext> databaseAction)
    {
        if (DbContext == null)
        {
            using (DbContext = ReturnDatabase())
            {
                databaseAction.Invoke(DbContext);
            }
        }
        else
        {
            databaseAction.Invoke(DbContext);
        }
    }

That works well in the whole program, just when i work in my program than it sometimes happens

OK the reason is strange as the bug self.

There is a other entity called: ServerBatch when i added them the first time, everything is OK. Then i edit them and add them the 2nd time to my list of Entitys. After that SaveChanges crash because i try to add the Entity with a PK to a list where the PK already exists. This crash is handled so the program will keep running and right after that crash the Entry method will fail and produce that Exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM