Entity framework EF:EnsureReloatedForContext方法
我正在阅读EF的源代码,在下面找到了这个方法。根据方法名称,它确保加载了dbcontext。当我使用EF Codefirst sample测试此方法时,此方法将被添加到当前程序集(我的示例控制台)到“_known组件” 我没有看到任何加载程序集的代码。我没有看到任何检查程序集是否加载的代码 这是命名问题还是我遗漏了什么?提前谢谢Entity framework EF:EnsureReloatedForContext方法,entity-framework,Entity Framework,我正在阅读EF的源代码,在下面找到了这个方法。根据方法名称,它确保加载了dbcontext。当我使用EF Codefirst sample测试此方法时,此方法将被添加到当前程序集(我的示例控制台)到“_known组件” 我没有看到任何加载程序集的代码。我没有看到任何检查程序集是否加载的代码 这是命名问题还是我遗漏了什么?提前谢谢 public virtual void EnsureLoadedForContext(Type contextType) {
public virtual void EnsureLoadedForContext(Type contextType)
{
DebugCheck.NotNull(contextType);
Debug.Assert(typeof(DbContext).IsAssignableFrom(contextType));
var contextAssembly = contextType.Assembly;
if (contextType == typeof(DbContext)
|| _knownAssemblies.ContainsKey(contextAssembly))
{
return;
}
if (_configurationOverrides.IsValueCreated)
{
lock (_lock)
{
if (_configurationOverrides.Value.Count != 0)
{
return;
}
}
}
if (!ConfigurationSet)
{
var foundConfigurationType =
_loader.TryLoadFromConfig(AppConfig.DefaultInstance) ??
_finder.TryFindConfigurationType(contextType);
if (foundConfigurationType != null)
{
SetConfigurationType(foundConfigurationType);
}
}
else if (!contextAssembly.IsDynamic // Don't throw for proxy contexts created in dynamic assemblies
&& !_loader.AppConfigContainsDbConfigurationType(AppConfig.DefaultInstance))
{
var foundType = _finder.TryFindConfigurationType(contextType);
if (foundType != null)
{
if (_configuration.Value.Owner.GetType() == typeof(DbConfiguration))
{
throw new InvalidOperationException(Strings.ConfigurationNotDiscovered(foundType.Name));
}
if (foundType != _configuration.Value.Owner.GetType())
{
throw new InvalidOperationException(
Strings.SetConfigurationNotDiscovered(_configuration.Value.Owner.GetType().Name, contextType.Name));
}
}
}
_knownAssemblies.TryAdd(contextAssembly, null);
}
方法
EnsureLoadedForContext
不加载上下文,而是加载传递给该方法的上下文类型的配置。当您使用创建该方法的类型名(DbConfigurationManager.EnsureLoadedForContext
)查看该方法的名称时,更清楚的是,该方法与加载配置相关,而不是加载上下文。最后,您可以查看其中一条的注释,内容如下:
EnsureReloadedForContext在知道上下文类型后立即从不同位置调用,以确保找到正确的DbConfiguration
对“DbConfigurationManager.EnsureLoadedForContext)”现在对我来说更有意义。。谢谢