找回密码
 立即注册
首页 业界区 业界 如何用Roslyn干掉令人烦躁的硬编码Dbse

如何用Roslyn干掉令人烦躁的硬编码Dbse

班嘉淑 昨天 16:34
.net中使用efcore作为orm的项目有很多,efcore有很多优点,特别是小型项目里使用linq操作数据库的丝滑。但是有时候我们又不得不面对一些比较恶心的名场面,比如硬编码Dbset。众所周知efcontext是基于其类型里定义的Dbset来追踪实体的。所以大部分时候不得不编写大量的public dbset user{get;set;}来让我们在仓储or服务层可以丝滑的调用_context.user.xxx;那么有没有办法避免硬编码呢?有的小伙伴说可以使用反射运行时装配到efcontext,不过反射有一个问题就是运行时确实可以装配进去了,但是开发时你只有使用_context.set来调用对象执行Linq,这就不符合初衷了,那么有没有办法既可以在业务代码层面_context.user.xxx同时又避免写硬编码呢?答案是有的,那就是基于Roslyn+SourceGenerator来实现,用了它不光编译时可以自动把那堆 dbset给你补上,重要的是你的IDE还可以自动化识别到这些变化,香得一匹。接下来我们就讲一下这个实现的原理,注意本文仅限.NET 6+ SDK / VS 2022 及更高版本
首先我们需要知道.net5的SourceGenerator这到底是个什么玩意儿,简单来讲它就是一个编译期帮你写代码的东西,避免运行时的反射开销比如什么AOP啊,什么胶水代码啊,什么代理类啊,都可以在编译期一次性生成,避免运行时用emit/表达式树等等动态植入的开销。所以我们的问题就变成了,如何让在efcontext中植入dbset。不过这里有不熟悉这个技术的小伙伴可能以为是类似直接在编译前读取.cs代码,然后把新内容硬编码到文件再编译?不过可惜SourceGenerator并不支持对编译的代码进行“魔改”,只能新增代码!! 所以我们只能另辟蹊径通过partial关键字把我们的efcontext拆成多个文件,其中主文件就是你自己的efcontext,而我们要生成的就是SourceGenerator的另外一份efcontext,当两份文件的命名空间和名字一样时,内部的属性函数会被看作是同一个类的。比如你的代码长这样:
  1.     public class MySqlEfContext : DbContext
  2.     {
  3.   <ItemGroup>
  4.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  5.   </ItemGroup>public MySqlEfContext(DbContextOptions<MySqlEfContext> options) : base(options)
  6.   <ItemGroup>
  7.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  8.   </ItemGroup>{
  9.   <ItemGroup>
  10.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  11.   </ItemGroup>}
  12.   <ItemGroup>
  13.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  14.   </ItemGroup>public MySqlEfContext()
  15.   <ItemGroup>
  16.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  17.   </ItemGroup>{
  18.   <ItemGroup>
  19.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  20.   </ItemGroup>}
  21.   <ItemGroup>
  22.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  23.   </ItemGroup>public DbSet<User> User { get; set; }
  24.   <ItemGroup>
  25.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  26.   </ItemGroup>....//一堆其他的硬编码Dbset
  27. }
复制代码
那么首先要做的修改就是删除你的Dbset,同时在类型定义上添加partial变成:
  1. public partial class MySqlEfContext : DbContext
复制代码
接着我们就可以基于Roslyn来分析有那些类型可以被植入到代码里把这些类型抽取出来,最后用SourceGenerator来编写一个新的efcontext.g.cs文件然后一同编译即可
首先我们需要创建一个新的类库.csproj。然后修改csproj的xml文件引入如下内容:
  1. <Project Sdk="Microsoft.NET.Sdk">
  2.   <PropertyGroup>
  3.     <TargetFramework>netstandard2.0</TargetFramework>
  4.     <ImplicitUsings>enable</ImplicitUsings>
  5.     <LangVersion>11.0</LangVersion>
  6.     <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
  7.   </PropertyGroup>
  8.   <ItemGroup>
  9.     <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
  10.     <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
  11.   </ItemGroup>
  12. </Project>
复制代码
  注意这里的TargetFramework必须是netstandard2.0!!!非常重要,否则你的IDE可能不会认识你的编译结果!!!
接着我们需要在efcontext所在的类库中引入这个新的类库,这样当MSBuild编译的时候就会先编译这个类库生成代码,然后再编译efcontext所在的类库,这样efcontext以及你其他业务代码引用_context.xxx.的部分就不会报编译错误了。打开你efcontext所在的.csproj
添加如下内容(这里我的文件夹和项目就叫EfContextGenerator你根据你实际创建的修改即可):
  1.   <ItemGroup>
  2.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  3.   </ItemGroup>
复制代码
这里的OutputItemType="Analyzer" ReferenceOutputAssembly="false"表示这是一个分析器项目,同时不需要编译到程序集中,必须要添加这两个tag否则编译会忽略
最后就是大头戏,在我们的EfContextGenerator项目下创建一个类文件(名字随意你喜好)。并在新添加的类上面继承这个接口:IIncrementalGenerator并同时给类打上特性:[Generator]。接着在IIncrementalGenerator上左键-实现接口。一个基本的框架就搭好了:
  1.     [Generator]    public class DbsetGenerator : IIncrementalGenerator    {  <ItemGroup>
  2.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  3.   </ItemGroup>public void Initialize(IncrementalGeneratorInitializationContext context)  <ItemGroup>
  4.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  5.   </ItemGroup>{  <ItemGroup>
  6.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  7.   </ItemGroup>    throw new NotImplementedException();  <ItemGroup>
  8.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  9.   </ItemGroup>}    }
复制代码
然后就是搭架子了。作为测试你可以编写一个硬编码的内容,看看你的代码是否可以正常工作,比如编写如下内容(把你的原始efcontext字符串拷贝进来):
  1. public void Initialize(IncrementalGeneratorInitializationContext ctx){    //Debugger.Launch();    ctx.RegisterSourceOutput(ctx.CompilationProvider, (spc, compilation) =>    {  <ItemGroup>
  2.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  3.   </ItemGroup>var generated = """  <ItemGroup>
  4.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  5.   </ItemGroup>using Domain.DomainBase;  <ItemGroup>
  6.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  7.   </ItemGroup>using Infrastructure.DataBase.PO;  <ItemGroup>
  8.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  9.   </ItemGroup>using InfrastructureBase;  <ItemGroup>
  10.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  11.   </ItemGroup>using Microsoft.Data.SqlClient;  <ItemGroup>
  12.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  13.   </ItemGroup>using Microsoft.EntityFrameworkCore;  <ItemGroup>
  14.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  15.   </ItemGroup>using Microsoft.Extensions.Configuration;  <ItemGroup>
  16.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  17.   </ItemGroup>using System;  <ItemGroup>
  18.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  19.   </ItemGroup>using System.Reflection;  <ItemGroup>
  20.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  21.   </ItemGroup>namespace Infrastructure.DataBase  <ItemGroup>
  22.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  23.   </ItemGroup>{  <ItemGroup>
  24.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  25.   </ItemGroup>    public partial class MySqlEfContext : DbContext  <ItemGroup>
  26.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  27.   </ItemGroup>    {  <ItemGroup>
  28.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  29.   </ItemGroup>  <ItemGroup>
  30.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  31.   </ItemGroup>  <ItemGroup>
  32.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  33.   </ItemGroup>    }  <ItemGroup>
  34.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  35.   </ItemGroup>    public DbSet User { get; set; }  <ItemGroup>
  36.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  37.   </ItemGroup>    public DbSet Permission { get; set; }  <ItemGroup>
  38.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  39.   </ItemGroup>    public DbSet Role { get; set; }  <ItemGroup>
  40.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  41.   </ItemGroup>}  <ItemGroup>
  42.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  43.   </ItemGroup>""";  <ItemGroup>
  44.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  45.   </ItemGroup>spc.AddSource("MySqlEfContext.DbSets.g.cs", SourceText.From(generated, Encoding.UTF8));    });}
复制代码
 保存后理论上你的IDE(我的是vs2022)会自动编译一次,如果你没有触发编译可以右键解决方案编译一次。理论上你的仓储或者服务层原来的那些context.user/role调用不会抛IDE错误:
1.png

 如果出现这个错误,请回头看我上面的步骤确定每一步都是按照我的方式写的理论上应该可以解决该问题:
2.png

 如果一切无碍,那么接下来就是通过动态生成generated来替换掉刚才硬编码的部分,由于编译期是没有办法反射的(虽然有办法,但是完全不推荐,太伤脑子了,各种引入各种反模式,最佳实践一定是基于roslyn的AST语法树分析实现),所以这里我们需要分析代码的语法树,从语法树中查询对应的节点是否具备某些我们需要的内容来决定是否读取该类型的名称。这里以我的系统为例。我的所有实体保存到namespace Infrastructure.DataBase.PO这个命名空间中,有两种实体继承,一种是继承领域模型,一种是持久化模型(即某些连接型实体不涉及业务的):
  1. //有领域模型继承领域:namespace Infrastructure.DataBase.PO{    public class AppBuildingProject : Domain.Entities.AppBuildingProject    {    }}//关联表或者领域子实体比如订单明细,只有持久化的部分:namespace Infrastructure.DataBase.PO{    public class ApplicationUserPermission : PersistenceObjectBase    {  <ItemGroup>
  2.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  3.   </ItemGroup>public int AppId { get; set; }  <ItemGroup>
  4.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  5.   </ItemGroup>public int UserId { get; set; }  <ItemGroup>
  6.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  7.   </ItemGroup>public int PermissionId { get; set; }    }}
复制代码
我们要做的其实就是和平时反射一样,去找特定的命名空间,检查继承的父类,然后抽取名字,最后用模板代码生成刚才那段硬编码的内容,最后大功告成。具体代码我都写了注释,基本上照着看都能明白啥意思,都不是很复杂的内容:
  1. namespace EfContextGenerator{    [Generator]    public class DbsetGenerator : IIncrementalGenerator    {  <ItemGroup>
  2.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  3.   </ItemGroup>public void Initialize(IncrementalGeneratorInitializationContext ctx)  <ItemGroup>
  4.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  5.   </ItemGroup>{  <ItemGroup>
  6.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  7.   </ItemGroup>    //Debugger.Launch();  <ItemGroup>
  8.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  9.   </ItemGroup>    // 拿到整个 Compilation  <ItemGroup>
  10.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  11.   </ItemGroup>    var compilationProvider = ctx.CompilationProvider;  <ItemGroup>
  12.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  13.   </ItemGroup>    ctx.RegisterSourceOutput(compilationProvider, (spc, compilation) =>  <ItemGroup>
  14.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  15.   </ItemGroup>    {  <ItemGroup>
  16.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  17.   </ItemGroup>  <ItemGroup>
  18.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  19.   </ItemGroup>// 1. 找到“持久层基类”符号  <ItemGroup>
  20.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  21.   </ItemGroup>  <ItemGroup>
  22.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  23.   </ItemGroup>var persistenceBase = compilation  <ItemGroup>
  24.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  25.   </ItemGroup>  <ItemGroup>
  26.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  27.   </ItemGroup>    .GetTypeByMetadataName("Infrastructure.Data.PersistenceObjectBase");  <ItemGroup>
  28.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  29.   </ItemGroup>  <ItemGroup>
  30.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  31.   </ItemGroup>// 2. 收集所有类型  <ItemGroup>
  32.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  33.   </ItemGroup>  <ItemGroup>
  34.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  35.   </ItemGroup>var allTypes = CollectAllTypes(compilation);  <ItemGroup>
  36.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  37.   </ItemGroup>  <ItemGroup>
  38.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  39.   </ItemGroup>var targetNamespace = "Infrastructure.DataBase.PO";  <ItemGroup>
  40.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  41.   </ItemGroup>  <ItemGroup>
  42.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  43.   </ItemGroup>// 3. 过滤出我们关心的两类  <ItemGroup>
  44.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  45.   </ItemGroup>  <ItemGroup>
  46.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  47.   </ItemGroup>var entityTypes = allTypes.Where(t => t.ContainingNamespace.ToDisplayString() == targetNamespace && (InheritsFromDomainEntities(t) || InheritsFrom(t, persistenceBase))).ToList();  <ItemGroup>
  48.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  49.   </ItemGroup>  <ItemGroup>
  50.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  51.   </ItemGroup>// 4. 根据筛选结果,生成 DbSet 属性  <ItemGroup>
  52.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  53.   </ItemGroup>  <ItemGroup>
  54.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  55.   </ItemGroup>var generated = GenerateDbContextPartial(entityTypes,  <ItemGroup>
  56.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  57.   </ItemGroup>  <ItemGroup>
  58.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  59.   </ItemGroup>    @namespace: "Infrastructure.DataBase",  <ItemGroup>
  60.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  61.   </ItemGroup>  <ItemGroup>
  62.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  63.   </ItemGroup>    dbContextName: "MySqlEfContext");  <ItemGroup>
  64.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  65.   </ItemGroup>  <ItemGroup>
  66.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  67.   </ItemGroup>spc.AddSource("MySqlEfContext.DbSets.g.cs", SourceText.From(generated, Encoding.UTF8));  <ItemGroup>
  68.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  69.   </ItemGroup>    });  <ItemGroup>
  70.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  71.   </ItemGroup>}  <ItemGroup>
  72.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  73.   </ItemGroup>///  <ItemGroup>
  74.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  75.   </ItemGroup> /// 遍历全局命名空间,收集所有类型(包括嵌套类型)  <ItemGroup>
  76.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  77.   </ItemGroup>///  <ItemGroup>
  78.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  79.   </ItemGroup> static List CollectAllTypes(Compilation compilation)  <ItemGroup>
  80.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  81.   </ItemGroup>{  <ItemGroup>
  82.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  83.   </ItemGroup>    var result = new List();  <ItemGroup>
  84.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  85.   </ItemGroup>    var stack = new Stack();  <ItemGroup>
  86.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  87.   </ItemGroup>    stack.Push(compilation.GlobalNamespace);  <ItemGroup>
  88.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  89.   </ItemGroup>    while (stack.Count > 0)  <ItemGroup>
  90.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  91.   </ItemGroup>    {  <ItemGroup>
  92.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  93.   </ItemGroup>  <ItemGroup>
  94.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  95.   </ItemGroup>var ns = stack.Pop();  <ItemGroup>
  96.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  97.   </ItemGroup>  <ItemGroup>
  98.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  99.   </ItemGroup>foreach (var member in ns.GetMembers())  <ItemGroup>
  100.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  101.   </ItemGroup>  <ItemGroup>
  102.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  103.   </ItemGroup>{  <ItemGroup>
  104.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  105.   </ItemGroup>  <ItemGroup>
  106.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  107.   </ItemGroup>    if (member is INamespaceSymbol childNs)  <ItemGroup>
  108.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  109.   </ItemGroup>  <ItemGroup>
  110.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  111.   </ItemGroup>    {  <ItemGroup>
  112.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  113.   </ItemGroup>  <ItemGroup>
  114.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  115.   </ItemGroup>  <ItemGroup>
  116.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  117.   </ItemGroup>stack.Push(childNs);  <ItemGroup>
  118.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  119.   </ItemGroup>  <ItemGroup>
  120.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  121.   </ItemGroup>    }  <ItemGroup>
  122.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  123.   </ItemGroup>  <ItemGroup>
  124.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  125.   </ItemGroup>    else if (member is INamedTypeSymbol typeSym)  <ItemGroup>
  126.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  127.   </ItemGroup>  <ItemGroup>
  128.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  129.   </ItemGroup>    {  <ItemGroup>
  130.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  131.   </ItemGroup>  <ItemGroup>
  132.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  133.   </ItemGroup>  <ItemGroup>
  134.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  135.   </ItemGroup>CollectNested(typeSym, result);  <ItemGroup>
  136.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  137.   </ItemGroup>  <ItemGroup>
  138.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  139.   </ItemGroup>    }  <ItemGroup>
  140.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  141.   </ItemGroup>  <ItemGroup>
  142.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  143.   </ItemGroup>}  <ItemGroup>
  144.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  145.   </ItemGroup>    }  <ItemGroup>
  146.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  147.   </ItemGroup>    return result;  <ItemGroup>
  148.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  149.   </ItemGroup>}  <ItemGroup>
  150.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  151.   </ItemGroup>///  <ItemGroup>
  152.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  153.   </ItemGroup> /// 递归收集类型及其所有嵌套类型  <ItemGroup>
  154.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  155.   </ItemGroup>///  <ItemGroup>
  156.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  157.   </ItemGroup> static void CollectNested(INamedTypeSymbol sym, List output)  <ItemGroup>
  158.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  159.   </ItemGroup>{  <ItemGroup>
  160.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  161.   </ItemGroup>    output.Add(sym);  <ItemGroup>
  162.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  163.   </ItemGroup>    foreach (var nested in sym.GetTypeMembers())  <ItemGroup>
  164.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  165.   </ItemGroup>  <ItemGroup>
  166.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  167.   </ItemGroup>CollectNested(nested, output);  <ItemGroup>
  168.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  169.   </ItemGroup>}  <ItemGroup>
  170.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  171.   </ItemGroup>///  <ItemGroup>
  172.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  173.   </ItemGroup> /// 判断某类型是否继承自指定基类(任何层级)  <ItemGroup>
  174.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  175.   </ItemGroup>///  <ItemGroup>
  176.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  177.   </ItemGroup> static bool InheritsFrom(INamedTypeSymbol sym, INamedTypeSymbol baseType)  <ItemGroup>
  178.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  179.   </ItemGroup>{  <ItemGroup>
  180.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  181.   </ItemGroup>    if (baseType == null) return false;  <ItemGroup>
  182.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  183.   </ItemGroup>    var cur = sym.BaseType;  <ItemGroup>
  184.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  185.   </ItemGroup>    while (cur != null)  <ItemGroup>
  186.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  187.   </ItemGroup>    {  <ItemGroup>
  188.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  189.   </ItemGroup>  <ItemGroup>
  190.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  191.   </ItemGroup>if (SymbolEqualityComparer.Default.Equals(cur, baseType))  <ItemGroup>
  192.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  193.   </ItemGroup>  <ItemGroup>
  194.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  195.   </ItemGroup>    return true;  <ItemGroup>
  196.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  197.   </ItemGroup>  <ItemGroup>
  198.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  199.   </ItemGroup>cur = cur.BaseType;  <ItemGroup>
  200.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  201.   </ItemGroup>    }  <ItemGroup>
  202.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  203.   </ItemGroup>    return false;  <ItemGroup>
  204.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  205.   </ItemGroup>}  <ItemGroup>
  206.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  207.   </ItemGroup>///  <ItemGroup>
  208.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  209.   </ItemGroup> /// 判断某类型的继承链里,是否有类型命名空间以 Domain.Entities 开头  <ItemGroup>
  210.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  211.   </ItemGroup>///  <ItemGroup>
  212.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  213.   </ItemGroup> static bool InheritsFromDomainEntities(INamedTypeSymbol sym)  <ItemGroup>
  214.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  215.   </ItemGroup>{  <ItemGroup>
  216.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  217.   </ItemGroup>    var cur = sym.BaseType;  <ItemGroup>
  218.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  219.   </ItemGroup>    while (cur != null)  <ItemGroup>
  220.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  221.   </ItemGroup>    {  <ItemGroup>
  222.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  223.   </ItemGroup>  <ItemGroup>
  224.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  225.   </ItemGroup>var ns = cur.ContainingNamespace?.ToDisplayString();  <ItemGroup>
  226.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  227.   </ItemGroup>  <ItemGroup>
  228.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  229.   </ItemGroup>if (ns != null && ns.StartsWith("Domain.Entities", StringComparison.Ordinal))  <ItemGroup>
  230.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  231.   </ItemGroup>  <ItemGroup>
  232.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  233.   </ItemGroup>    return true;  <ItemGroup>
  234.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  235.   </ItemGroup>  <ItemGroup>
  236.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  237.   </ItemGroup>cur = cur.BaseType;  <ItemGroup>
  238.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  239.   </ItemGroup>    }  <ItemGroup>
  240.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  241.   </ItemGroup>    return false;  <ItemGroup>
  242.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  243.   </ItemGroup>}  <ItemGroup>
  244.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  245.   </ItemGroup>///  <ItemGroup>
  246.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  247.   </ItemGroup> /// 根据筛选后的实体列表,拼出 DbContext 部分类的源码  <ItemGroup>
  248.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  249.   </ItemGroup>///  <ItemGroup>
  250.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  251.   </ItemGroup> static string GenerateDbContextPartial(  <ItemGroup>
  252.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  253.   </ItemGroup>    List entities,  <ItemGroup>
  254.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  255.   </ItemGroup>    string @namespace,  <ItemGroup>
  256.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  257.   </ItemGroup>    string dbContextName)  <ItemGroup>
  258.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  259.   </ItemGroup>{  <ItemGroup>
  260.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  261.   </ItemGroup>    var sb = new StringBuilder();  <ItemGroup>
  262.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  263.   </ItemGroup>    sb.AppendLine("// ");  <ItemGroup>
  264.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  265.   </ItemGroup>    sb.AppendLine("using Domain.DomainBase;");  <ItemGroup>
  266.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  267.   </ItemGroup>    sb.AppendLine("using Infrastructure.DataBase.PO;");  <ItemGroup>
  268.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  269.   </ItemGroup>    sb.AppendLine("using InfrastructureBase;");  <ItemGroup>
  270.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  271.   </ItemGroup>    sb.AppendLine("using Microsoft.Data.SqlClient;");  <ItemGroup>
  272.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  273.   </ItemGroup>    sb.AppendLine("using Microsoft.EntityFrameworkCore;");  <ItemGroup>
  274.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  275.   </ItemGroup>    sb.AppendLine("using Microsoft.Extensions.Configuration;");  <ItemGroup>
  276.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  277.   </ItemGroup>    sb.AppendLine("using System;");  <ItemGroup>
  278.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  279.   </ItemGroup>    sb.AppendLine("using System.Reflection;");  <ItemGroup>
  280.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  281.   </ItemGroup>    sb.AppendLine($"namespace {@namespace};");  <ItemGroup>
  282.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  283.   </ItemGroup>    sb.AppendLine();  <ItemGroup>
  284.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  285.   </ItemGroup>    sb.AppendLine($"public partial class {dbContextName} : DbContext");  <ItemGroup>
  286.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  287.   </ItemGroup>    sb.AppendLine("{");  <ItemGroup>
  288.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  289.   </ItemGroup>    foreach (var e in entities)  <ItemGroup>
  290.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  291.   </ItemGroup>    {  <ItemGroup>
  292.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  293.   </ItemGroup>  <ItemGroup>
  294.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  295.   </ItemGroup>var name = e.Name;  <ItemGroup>
  296.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  297.   </ItemGroup>  <ItemGroup>
  298.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  299.   </ItemGroup>sb.AppendLine($"    public DbSet {name} {{ get; set; }}");  <ItemGroup>
  300.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  301.   </ItemGroup>    }  <ItemGroup>
  302.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  303.   </ItemGroup>    sb.AppendLine("}");  <ItemGroup>
  304.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  305.   </ItemGroup>    return sb.ToString();  <ItemGroup>
  306.     <ProjectReference Include="..\EfContextGenerator\EfContextGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  307.   </ItemGroup>}    }}
复制代码
如果你走到这一步那么基本上所有的工作就完成了,剩下的你可以尝试在新建一个实体,立刻就可以被IDE识别到啦:
3.png

4.png

5.png

 分享就到这里,剩下的就小伙伴们去尝试吧~
 
[code][/code] 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册