如何停止EF检测我的数据库/模型的更改

3 entity-framework

我的代码中有以下内容:

 DbContext = new DataContext();
 DbContext.Configuration.AutoDetectChangesEnabled = false;
Run Code Online (Sandbox Code Playgroud)

然后在我的控制器方法中:

// GET /api/Applications
public IEnumerable<Application> Get()
{
    return _uow.Applications.GetAll().OrderBy(p => p.Name);
}
Run Code Online (Sandbox Code Playgroud)

但是我仍然收到以下消息:

System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=The model backing the 'DataContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
  Source=EntityFramework
  StackTrace:
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么会这样.我认为创建上下文后的行会停止此检查.

cin*_*net 9

你可以放置(这是一个静态的方法)

Database.SetInitializer<DataContext>(null);
Run Code Online (Sandbox Code Playgroud)

在你第一次使用之前DataContext.但请注意,这将关闭它.完成.如果您的数据库与model不兼容,则查询等将失败.