大叹号 發表於 2025-9-6 18:48:00

【EF Core】实体类的依赖注入

<p>在使用外部模型那篇水文中,有大伙伴提出:老周,你那个 Ultraman 类和 Speciality 类的的关系是不是有问题,外键不应该在 Speciality 类上吗,怎么会跑到 Ultraman 类上?因为它们是一对一关系,在配置的时候你也可以反过来,主要区别是谁引用谁的问题,由于是一对一引用,所以反过来也可以的。</p>
<p>今天咱们聊聊实体类构造函数的依赖注入。实体类也支持依赖注入,不过,目前版本只支持注入 EF Core 自己的服务类型,你在代码中添加的应用程序级服务类型不能注入(以后可能会支持)。总结一下,以下服务类型可以注入:</p>
<p>1、EF Core 框架内部注册的服务类型;</p>
<p>2、三个补充类型:</p>
<p>  a、当前 DbContext 实例(你从 DbContext 派生的类);</p>
<p>  b、当前实体相关的 IEntityType,可以获取实体模型相关信息;</p>
<p>  c、ILazyLoader 接口,延时加载时用得上。</p>
<p>&nbsp;</p>
<p>现在要解决一个问题:我怎么知道 EF Core 框架内部哪些服务可以注入?这个活儿有两个方案:第一个方案是看 EF Core 源代码,在&nbsp;EFCore\Infrastructure 下,注意看&nbsp;EntityFrameworkServicesBuilder 类的&nbsp;CoreServices 字段或&nbsp;TryAddCoreServices 方法,基本齐全了。</p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">readonly</span> IDictionary&lt;Type, ServiceCharacteristics&gt;<span style="color: rgba(0, 0, 0, 1)"> CoreServices
      </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Dictionary&lt;Type, ServiceCharacteristics&gt;<span style="color: rgba(0, 0, 0, 1)">
      {
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(<strong><span style="background-color: rgba(255, 255, 153, 1)">LoggingDefinitions</span></strong>), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(<strong><span style="background-color: rgba(255, 255, 153, 1)">IDatabaseProvider</span></strong>), <span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(<strong><span style="background-color: rgba(255, 255, 153, 1)">IDbSetFinder</span></strong>), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(<strong><span style="background-color: rgba(255, 255, 153, 1)">IDbSetInitializer</span></strong>), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDbSetSource), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IEntityFinderSource), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IStructuralTypeMaterializerSource), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ITypeMappingSource), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IModelCustomizer), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IModelCacheKeyFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IModelSource), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IModelRuntimeInitializer), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IInternalEntrySubscriber), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IEntityEntryGraphIterator), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IValueGeneratorCache), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ISingletonOptionsInitializer), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ILoggingOptions), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ICoreSingletonOptions), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IModelValidator), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ICompiledQueryCache), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IValueConverterSelector), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IConstructorBindingFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IRegisteredServices), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IPropertyParameterBindingFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IParameterBindingFactories), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IMemberClassifier), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IMemoryCache), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IEvaluatableExpressionFilter), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(INavigationExpansionExtensibilityHelper), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ILiftableConstantFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IExceptionDetector), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IJsonValueReaderWriterSource), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Singleton) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IProviderConventionSetBuilder), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IConventionSetBuilder), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDiagnosticsLogger&lt;&gt;), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IInterceptors), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ILoggerFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IEntityGraphAttacher), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IKeyPropagator), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(INavigationFixer), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ILocalViewListener), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IStateManager), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IConcurrencyDetector), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IInternalEntityEntryNotifier), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IValueGenerationManager), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IChangeTrackerFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IChangeDetector), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDbContextServices), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IValueGeneratorSelector), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IExecutionStrategyFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IExecutionStrategy), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IAsyncQueryProvider), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IQueryCompiler), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ICompiledQueryCacheKeyGenerator), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IModel), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDesignTimeModel), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IUpdateAdapterFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ICurrentDbContext), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDbContextDependencies), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDatabaseFacadeDependencies), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDbContextOptions), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDatabase), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDatabaseCreator), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDbContextTransactionManager), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IQueryContextFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IQueryCompilationContextFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IQueryableMethodTranslatingExpressionVisitorFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IQueryTranslationPreprocessorFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IQueryTranslationPostprocessorFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IShapedQueryCompilingExpressionVisitorFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IDbContextLogger), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IAdHocMapper), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ILiftableConstantProcessor), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ILazyLoader), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Transient) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ILazyLoaderFactory), <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ServiceCharacteristics(ServiceLifetime.Scoped) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IParameterBindingFactory), <span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ITypeMappingSourcePlugin), <span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) },
            {
                </span><span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(IEvaluatableExpressionFilterPlugin),
                </span><span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)
            },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ISingletonOptions), <span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IConventionSetPlugin), <span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Scoped, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(ISingletonInterceptor), <span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IResettableService), <span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Scoped, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IInterceptor), <span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Scoped, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) },
            { </span><span style="color: rgba(0, 0, 255, 1)">typeof</span>(IInterceptorAggregator), <span style="color: rgba(0, 0, 255, 1)">new</span> ServiceCharacteristics(ServiceLifetime.Scoped, multipleRegistrations: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">) }
      };</span></pre>
</div>
<p>这个字典的 Key 就是核心服务的类型。Relational 库和数据库提供者可能会补充一些服务,但大都多是替换服务,即接口类型是不变的。</p>
<p>第二个方案是通过一个叫&nbsp;IRegisteredServices 的服务,它可以列出已注册服务的 Type 列表。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">using</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> c = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DemoContext())
{
    <strong><span style="background-color: rgba(255, 255, 0, 1)">IRegisteredServices regsvcs </span></strong></span><strong><span style="background-color: rgba(255, 255, 0, 1)">= c.GetService&lt;IRegisteredServices&gt;<span style="color: rgba(0, 0, 0, 1)">
();
</span></span></strong>    <span style="color: rgba(0, 0, 255, 1)">foreach</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> sv <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> regsvcs.<span style="background-color: rgba(255, 255, 0, 1)">Services</span>)
    {
      Console.WriteLine(sv);
    }
    ……
}</span></pre>
</div>
<p>这样就会列出内部已注册服务的类型。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">Microsoft.EntityFrameworkCore.Diagnostics.LoggingDefinitions
Microsoft.EntityFrameworkCore.Storage.IDatabaseProvider
Microsoft.EntityFrameworkCore.ValueGeneration.IValueGeneratorCache
Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMappingSource
Microsoft.EntityFrameworkCore.Storage.ISqlGenerationHelper
Microsoft.EntityFrameworkCore.Metadata.IRelationalAnnotationProvider
Microsoft.EntityFrameworkCore.Migrations.IMigrationsAnnotationProvider
Microsoft.EntityFrameworkCore.Infrastructure.IModelValidator
Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure.IProviderConventionSetBuilder
Microsoft.EntityFrameworkCore.Update.IUpdateSqlGenerator
Microsoft.EntityFrameworkCore.Query.IEvaluatableExpressionFilter
Microsoft.EntityFrameworkCore.Storage.IRelationalTransactionFactory
Microsoft.EntityFrameworkCore.Update.IModificationCommandBatchFactory
Microsoft.EntityFrameworkCore.Update.IModificationCommandFactory
Microsoft.EntityFrameworkCore.ValueGeneration.IValueGeneratorSelector
Microsoft.EntityFrameworkCore.Storage.IRelationalConnection
Microsoft.EntityFrameworkCore.Migrations.IMigrationsSqlGenerator
Microsoft.EntityFrameworkCore.Storage.IRelationalDatabaseCreator
Microsoft.EntityFrameworkCore.Migrations.IHistoryRepository
Microsoft.EntityFrameworkCore.Storage.IExecutionStrategyFactory
Microsoft.EntityFrameworkCore.Query.IRelationalQueryStringFactory
Microsoft.EntityFrameworkCore.Query.ICompiledQueryCacheKeyGenerator
Microsoft.EntityFrameworkCore.Query.IQueryCompilationContextFactory
Microsoft.EntityFrameworkCore.Query.IMethodCallTranslatorProvider
Microsoft.EntityFrameworkCore.Query.IAggregateMethodCallTranslatorProvider
Microsoft.EntityFrameworkCore.Query.IMemberTranslatorProvider
Microsoft.EntityFrameworkCore.Query.IQuerySqlGeneratorFactory
Microsoft.EntityFrameworkCore.Query.IRelationalSqlTranslatingExpressionVisitorFactory
Microsoft.EntityFrameworkCore.Query.ISqlExpressionFactory
Microsoft.EntityFrameworkCore.Query.IQueryTranslationPostprocessorFactory
Microsoft.EntityFrameworkCore.Query.IRelationalParameterBasedSqlProcessorFactory
Microsoft.EntityFrameworkCore.Query.INavigationExpansionExtensibilityHelper
Microsoft.EntityFrameworkCore.Query.IQueryableMethodTranslatingExpressionVisitorFactory
Microsoft.EntityFrameworkCore.Storage.IExceptionDetector
Microsoft.EntityFrameworkCore.Infrastructure.ISingletonOptions
Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.ISqlServerSingletonOptions
Microsoft.EntityFrameworkCore.SqlServer.ValueGeneration.Internal.ISqlServerValueGeneratorCache
Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.ISqlServerUpdateSqlGenerator
Microsoft.EntityFrameworkCore.SqlServer.ValueGeneration.Internal.ISqlServerSequenceValueGeneratorFactory      
Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.ISqlServerConnection
Microsoft.EntityFrameworkCore.Storage.IParameterNameGeneratorFactory
System.Collections.Generic.IComparer`</span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">   
Microsoft.EntityFrameworkCore.Migrations.IMigrationsIdGenerator
Microsoft.EntityFrameworkCore.Update.Internal.IRowKeyValueFactoryFactory
Microsoft.EntityFrameworkCore.Update.Internal.IRowForeignKeyValueFactoryFactory
Microsoft.EntityFrameworkCore.Update.Internal.IRowIndexValueFactoryFactory
Microsoft.EntityFrameworkCore.Update.Internal.IRowIdentityMapFactory
Microsoft.EntityFrameworkCore.Infrastructure.IModelCustomizer
Microsoft.EntityFrameworkCore.Infrastructure.IModelRuntimeInitializer
Microsoft.EntityFrameworkCore.Migrations.IMigrator
Microsoft.EntityFrameworkCore.Migrations.IMigrationCommandExecutor
Microsoft.EntityFrameworkCore.Migrations.IMigrationsAssembly
Microsoft.EntityFrameworkCore.Storage.IDatabase
Microsoft.EntityFrameworkCore.Update.IBatchExecutor
Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory
Microsoft.EntityFrameworkCore.Storage.IRawSqlCommandBuilder
Microsoft.EntityFrameworkCore.Update.ICommandBatchPreparer
Microsoft.EntityFrameworkCore.Migrations.IMigrationsModelDiffer
Microsoft.EntityFrameworkCore.Storage.ITypeMappingSource
Microsoft.EntityFrameworkCore.Storage.IDatabaseCreator
Microsoft.EntityFrameworkCore.Storage.IDbContextTransactionManager
Microsoft.EntityFrameworkCore.Query.IQueryContextFactory
Microsoft.EntityFrameworkCore.Storage.Internal.INamedConnectionStringResolver
Microsoft.EntityFrameworkCore.Storage.IDatabaseFacadeDependencies
Microsoft.EntityFrameworkCore.Storage.IRelationalDatabaseFacadeDependencies
Microsoft.EntityFrameworkCore.Diagnostics.IRelationalConnectionDiagnosticsLogger
Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger`</span><span style="color: rgba(128, 0, 128, 1)">1</span>
Microsoft.EntityFrameworkCore.Diagnostics.IRelationalCommandDiagnosticsLogger
Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger`</span><span style="color: rgba(128, 0, 128, 1)">1</span>
Microsoft.EntityFrameworkCore.Diagnostics.IInterceptorAggregator
Microsoft.EntityFrameworkCore.Query.IShapedQueryCompilingExpressionVisitorFactory
Microsoft.EntityFrameworkCore.Query.IQueryTranslationPreprocessorFactory
Microsoft.EntityFrameworkCore.Metadata.IAdHocMapper
Microsoft.EntityFrameworkCore.Query.ISqlAliasManagerFactory
Microsoft.EntityFrameworkCore.Query.ILiftableConstantFactory
Microsoft.EntityFrameworkCore.Query.IRelationalLiftableConstantFactory
Microsoft.EntityFrameworkCore.Query.ILiftableConstantProcessor
Microsoft.EntityFrameworkCore.Storage.RelationalSqlGenerationHelperDependencies
Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceDependencies
Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidatorDependencies
Microsoft.EntityFrameworkCore.Update.UpdateSqlGeneratorDependencies
Microsoft.EntityFrameworkCore.Metadata.RelationalAnnotationProviderDependencies
Microsoft.EntityFrameworkCore.Migrations.MigrationsAnnotationProviderDependencies
Microsoft.EntityFrameworkCore.Storage.ParameterNameGeneratorDependencies
Microsoft.EntityFrameworkCore.Storage.RelationalTransactionFactoryDependencies
Microsoft.EntityFrameworkCore.Storage.RelationalCommandBuilderDependencies
Microsoft.EntityFrameworkCore.Query.QuerySqlGeneratorDependencies
Microsoft.EntityFrameworkCore.Query.RelationalEvaluatableExpressionFilterDependencies
Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelDependencies
Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelRuntimeInitializerDependencies
Microsoft.EntityFrameworkCore.Query.RelationalLiftableConstantExpressionDependencies
Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGeneratorDependencies
Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure.RelationalConventionSetBuilderDependencies
Microsoft.EntityFrameworkCore.Update.ModificationCommandBatchFactoryDependencies
Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparerDependencies
Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreatorDependencies
Microsoft.EntityFrameworkCore.Migrations.HistoryRepositoryDependencies
Microsoft.EntityFrameworkCore.Query.RelationalCompiledQueryCacheKeyGeneratorDependencies
Microsoft.EntityFrameworkCore.Query.RelationalMethodCallTranslatorProviderDependencies
Microsoft.EntityFrameworkCore.Query.RelationalAggregateMethodCallTranslatorProviderDependencies
Microsoft.EntityFrameworkCore.Query.RelationalMemberTranslatorProviderDependencies
Microsoft.EntityFrameworkCore.Query.SqlExpressionFactoryDependencies
Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitorDependencies
Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitorDependencies
Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitorDependencies
Microsoft.EntityFrameworkCore.Query.RelationalQueryTranslationPreprocessorDependencies
Microsoft.EntityFrameworkCore.Query.RelationalQueryTranslationPostprocessorDependencies
Microsoft.EntityFrameworkCore.Query.RelationalParameterBasedSqlProcessorDependencies
Microsoft.EntityFrameworkCore.Storage.RelationalConnectionDependencies
Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseDependencies
Microsoft.EntityFrameworkCore.Query.RelationalQueryContextDependencies
Microsoft.EntityFrameworkCore.Query.RelationalQueryCompilationContextDependencies
Microsoft.EntityFrameworkCore.Metadata.RelationalAdHocMapperDependencies
Microsoft.EntityFrameworkCore.Infrastructure.IDbSetFinder
Microsoft.EntityFrameworkCore.Internal.IDbSetInitializer
Microsoft.EntityFrameworkCore.Internal.IDbSetSource
Microsoft.EntityFrameworkCore.Internal.IEntityFinderSource
Microsoft.EntityFrameworkCore.Query.IEntityMaterializerSource
Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure.IConventionSetBuilder
Microsoft.EntityFrameworkCore.Infrastructure.IModelCacheKeyFactory
Microsoft.Extensions.Logging.ILoggerFactory
Microsoft.EntityFrameworkCore.Infrastructure.IModelSource
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IInternalEntityEntrySubscriber
Microsoft.EntityFrameworkCore.ChangeTracking.IEntityEntryGraphIterator
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IEntityGraphAttacher
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IKeyPropagator
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.INavigationFixer
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ILocalViewListener
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager
Microsoft.EntityFrameworkCore.Infrastructure.IConcurrencyDetector
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IInternalEntityEntryNotifier
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IValueGenerationManager
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IChangeTrackerFactory
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IChangeDetector
Microsoft.EntityFrameworkCore.Internal.IDbContextServices
Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies
Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy
Microsoft.EntityFrameworkCore.Query.Internal.ICompiledQueryCache
Microsoft.EntityFrameworkCore.Query.IAsyncQueryProvider
Microsoft.EntityFrameworkCore.Query.Internal.IQueryCompiler
Microsoft.EntityFrameworkCore.Internal.ISingletonOptionsInitializer
Microsoft.EntityFrameworkCore.Diagnostics.IDiagnosticsLogger`</span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">
Microsoft.EntityFrameworkCore.Diagnostics.IInterceptors
Microsoft.EntityFrameworkCore.Diagnostics.ILoggingOptions
Microsoft.EntityFrameworkCore.Infrastructure.ICoreSingletonOptions
Microsoft.EntityFrameworkCore.Metadata.IModel
Microsoft.EntityFrameworkCore.Metadata.IDesignTimeModel
Microsoft.EntityFrameworkCore.Infrastructure.ICurrentDbContext
Microsoft.EntityFrameworkCore.Infrastructure.IDbContextOptions
Microsoft.EntityFrameworkCore.Infrastructure.IResettableService
Microsoft.EntityFrameworkCore.Storage.ValueConversion.IValueConverterSelector
Microsoft.EntityFrameworkCore.Metadata.IConstructorBindingFactory
Microsoft.EntityFrameworkCore.Infrastructure.Internal.ILazyLoaderFactory
Microsoft.EntityFrameworkCore.Infrastructure.ILazyLoader
Microsoft.EntityFrameworkCore.Metadata.IParameterBindingFactories
Microsoft.EntityFrameworkCore.Metadata.Internal.IMemberClassifier
Microsoft.EntityFrameworkCore.Metadata.IPropertyParameterBindingFactory
Microsoft.EntityFrameworkCore.Metadata.IParameterBindingFactory
Microsoft.Extensions.Caching.Memory.IMemoryCache
Microsoft.EntityFrameworkCore.Update.IUpdateAdapterFactory
Microsoft.EntityFrameworkCore.Storage.Json.IJsonValueReaderWriterSource
Microsoft.EntityFrameworkCore.Diagnostics.IDbContextLogger
System.Diagnostics.DiagnosticSource
Microsoft.EntityFrameworkCore.Metadata.LazyLoaderParameterBindingFactoryDependencies
Microsoft.EntityFrameworkCore.Storage.DatabaseProviderDependencies
Microsoft.EntityFrameworkCore.Infrastructure.ModelSourceDependencies
Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorCacheDependencies
Microsoft.EntityFrameworkCore.Infrastructure.ModelValidatorDependencies
Microsoft.EntityFrameworkCore.Storage.TypeMappingSourceDependencies
Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizerDependencies
Microsoft.EntityFrameworkCore.Infrastructure.ModelCacheKeyFactoryDependencies
Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverterSelectorDependencies
Microsoft.EntityFrameworkCore.Query.Internal.EntityMaterializerSourceDependencies
Microsoft.EntityFrameworkCore.Query.EvaluatableExpressionFilterDependencies
Microsoft.EntityFrameworkCore.Infrastructure.RuntimeModelDependencies
Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializerDependencies
Microsoft.EntityFrameworkCore.Query.NavigationExpansionExtensibilityHelperDependencies
Microsoft.EntityFrameworkCore.Storage.Json.JsonValueReaderWriterSourceDependencies
Microsoft.EntityFrameworkCore.Query.LiftableConstantExpressionDependencies
Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure.ProviderConventionSetBuilderDependencies   
Microsoft.EntityFrameworkCore.Query.QueryCompilationContextDependencies
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManagerDependencies
Microsoft.EntityFrameworkCore.Storage.ExecutionStrategyDependencies
Microsoft.EntityFrameworkCore.Query.CompiledQueryCacheKeyGeneratorDependencies
Microsoft.EntityFrameworkCore.Query.QueryContextDependencies
Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitorDependencies
Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessorDependencies
Microsoft.EntityFrameworkCore.Query.QueryTranslationPostprocessorDependencies
Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitorDependencies
Microsoft.EntityFrameworkCore.ValueGeneration.ValueGeneratorSelectorDependencies
Microsoft.EntityFrameworkCore.Storage.DatabaseDependencies
Microsoft.EntityFrameworkCore.Infrastructure.ModelDependencies
Microsoft.EntityFrameworkCore.ModelCreationDependencies
Microsoft.EntityFrameworkCore.Metadata.AdHocMapperDependencies</span></pre>
</div>
<p>严重注意:<strong><span style="color: rgba(255, 0, 0, 1)">实体构造函数不能注入&nbsp;IRegisteredServices&nbsp;&nbsp;类型的服务,因为 EF Core 内部实现时,本身就是从&nbsp;IRegisteredServices.Services 集合中查找可以注入的服务的,而 IRegisteredServices.Services 是不包含它自己的。说人话就是:用 IRegisteredServices 类注入是找不到该服务的。</span></strong></p>
<p>&nbsp;</p>
<p>然后解决第二个问题:在实体类的构造函数中注入服务有啥用?大多数时候没啥用,但当你要使用某些服务来获取特殊信息,或执行特殊操作(如在实体类中执行 SQL 语句)时就用得上。</p>
<p>咱们举个例子,就用官方文档最喜欢的 Blog 和 Post 实体来演示。其中,Blog 类的构造函数将注入 IEntityType 服务。然后可以利用这个服务来获取 Blog 实体有几个属性,有几个键,有几个导航属性。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Blog
{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 私有字段</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> <strong><span style="background-color: rgba(255, 255, 0, 1)">IEntityType?<span style="color: rgba(0, 0, 0, 1)"> _thisEntity;
</span></span></strong>    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 私有构造器</span>
    <span style="color: rgba(0, 0, 255, 1)">private</span> Blog(<span style="background-color: rgba(255, 255, 0, 1)">IEntityType t</span>) =&gt; <span style="background-color: rgba(255, 255, 0, 1)">_thisEntity =</span><span style="color: rgba(0, 0, 0, 1)"><span style="background-color: rgba(255, 255, 0, 1)"> t</span>;

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 公共构造器</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Blog()
    {
    }

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> BlogID { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> DisplayName { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span>; } = <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> Url { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span>; } = <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty;

    </span><span style="color: rgba(0, 0, 255, 1)">public</span> List&lt;Post&gt; PostList { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span>; } = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)">();

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 以下属性通过注入的 IEntityType 获取相关值</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> PropertyCount =&gt; <span style="background-color: rgba(255, 255, 0, 1)">_thisEntity?.GetProperties().Count() ?? <span style="color: rgba(128, 0, 128, 1)">0</span></span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> NavigationCount =&gt; <span style="background-color: rgba(255, 255, 0, 1)">_thisEntity?.GetNavigations().Count() ?? <span style="color: rgba(128, 0, 128, 1)">0</span></span><span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> KeyCount =&gt; <span style="background-color: rgba(255, 255, 0, 1)">_thisEntity?.GetKeys().Count() ?? <span style="color: rgba(128, 0, 128, 1)">0</span></span><span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Post
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> PostID { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> Header { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span>; } = <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty;
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> Blog? TheBlog { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
}</span></pre>
</div>
<p>注意,你自己 new 一个 Blog 实例是没有注入功能的,所以要定义一个无参数的公共构造函数,以供外部初始化用。另一个私有构造函数有一个 IEntityType 类型的参数,可以接收注入。因为这构造函数只由 EF Core 内部调用,代码中调用无法注入,故声明为私有即可。EF Core 在查找构造函数时,不管你是公有的还是私有的,只要不是静态的,一律能发现。</p>
<p>在访问 Blog 实体时就可以获取键、属性、导航属性的数量。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> q = c.Blogs.Include(b =&gt;<span style="color: rgba(0, 0, 0, 1)"> b.PostList).ToArray();
</span><span style="color: rgba(0, 0, 255, 1)">var</span> first =<span style="color: rgba(0, 0, 0, 1)"> q.FirstOrDefault();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (first != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
    Console.WriteLine($</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">实体{first.GetType().Name}有{first.</span>
<span style="color: rgba(0, 0, 0, 1)">PropertyCount}个属性,{first.KeyCount}个键,{first.
NavigationCount}个导航属性</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">);</span>
}</pre>
</div>
<p>得到的输出如下:</p>
<div class="cnblogs_code">
<pre>实体Blog有3个属性,1个键,1个导航属性</pre>
</div>
<p>&nbsp;</p>
<p>除了构造函数,也可以从属性注入。例如</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Screen
{
   
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> ScrID { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
   
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> Width { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
   
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> Height { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">----------- 注意以下属性 --------------</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <strong><span style="background-color: rgba(255, 255, 0, 1)">MyDBContext? ThisContext</span></strong> { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"><span style="background-color: rgba(255, 255, 0, 1)"> MyDBContext</span> : DbContext
{
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> MyDBContext(DbContextOptions&lt;MyDBContext&gt;<span style="color: rgba(0, 0, 0, 1)"> options)
      : </span><span style="color: rgba(0, 0, 255, 1)">base</span><span style="color: rgba(0, 0, 0, 1)">(options)
    { }
      
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> DbSet&lt;Screen&gt; Screens { <span style="color: rgba(0, 0, 255, 1)">get</span>; <span style="color: rgba(0, 0, 255, 1)">set</span><span style="color: rgba(0, 0, 0, 1)">; }
}</span></pre>
</div>
<p>上面示例中,MyDBContext 实例会注入到每个 Screen 实例的 ThisContext 属性。即每个实体实例的 ThisContext 属性都引用同一个 MyDBContext 对象。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">var</span> q =<span style="color: rgba(0, 0, 0, 1)"> dc.Screens.FirstOrDefault();
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (q != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (q.ThisContext != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
    {
      Console.WriteLine(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ThisContext属性:{0}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, q.ThisContext);
    }
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输出:
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> ThisContext属性:MyDBContext</span></pre>
</div>
<p>好了,今天就水到这里了。因为这个主题确实太简单了,老周也不必举太多例子。</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/tcjiaan/p/19077173
頁: [1]
查看完整版本: 【EF Core】实体类的依赖注入