在 Asp.Net Core 5.0 中,当使用 Entity Framework Core 进行数据库操作时,可能会遇到一个空异常问题,该异常出现在使用 EntityState.Detached 进行实体对象分离操作时。具体表现为以下异常信息:
System.InvalidOperationException: An attempt was made to use the context while it is being configured. A DbContext instance cannot be used inside OnConfiguring since it is still being configured at this point. This can happen if a second operation is started on the context before a previous operation completed. Any instance members are not guaranteed to be thread safe.
该异常的解决方法是,在进行实体对象分离操作时,需要使用 DbSet