简体   繁体   English

实体框架 6.1.3 当 dbContext 处理的分离对象为空时

[英]Entity Framework 6.1.3 when dbContext disposed detached objects are null

The below code uses DBContext, and at times I get Subject[] array being null and can only assume that detached objects on disposal of the context has an effect on these same objects making their references null out of the scope the above mentioned.下面的代码使用 DBContext,有时我得到 Subject[] 数组为空,并且只能假设分离的对象在处理上下文时对这些相同的对象有影响,使它们的引用超出上述范围。 That's when the subject[] array is returned to the caller.那就是将 subject[] 数组返回给调用者的时候。 In the caller a reference to subjec[] at times returns null.在调用者中,对 subjec[] 的引用有时会返回 null。 Is this how it should be?这是应该的吗?

public Subject[] GetSubjectList()
{
    using (var dbContext = new DataAccess.TeachersAssistantDbContext())
    {
        _unitOfWork.InitializeDbContext(dbContext);
        return _unitOfWork._subjectRepository.GetAll();
    }
}

I was expecting since the dbContext is disposed, the objects get detached, and still should exist within the calling code provided there is a reference to them.我期待因为 dbContext 被释放,对象被分离,并且仍然应该存在于调用代码中,前提是有对它们的引用。 I am getting this error with Entity Framework 6.1.3我在使用实体框架 6.1.3 时遇到此错误

Sorry Chaps, I run migrations before, and looks like there was one pending migration that didn't run.抱歉,我之前运行过迁移,看起来有一个未运行的待处理迁移。 I checked, and indeed there was an addition to to the Model, and an Exception was being thrown about migrations expected due to inconsistency between database and dbContext.我检查过,确实有一个对模型的补充,并且由于数据库和 dbContext 之间的不一致而引发了关于预期迁移的异常。 I have ever since run the migrations and the problem has been resolved.从那以后我一直在运行迁移,问题已经解决。 Many thanks for you help though.非常感谢你的帮助。 Someone may end up in this awkward situation without realising the Model Entities changed and not all migrations were run.有人可能最终会陷入这种尴尬的境地,而没有意识到模型实体已更改且并非所有迁移都已运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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