zoukankan      html  css  js  c++  java
  • PowerShell 自动合并 db first 的dbcontext

    # ==============================实现步骤==============================
    # 第一步,获取Using,组装并去重
    # usings+(?<namespace>.*?);
    
    # 第二步,获取所有DbSet,组装并去重
    # publics+virtuals+DbSet<(?<entity>.*?)>.*?}
    
    # 第三步,获取所有的Entity声明,组装并去重
    # modelBuilder.Entity<(?<entity>.*)>.*
    *
    *s*{([^}]*)});
    
    # ==============================实现步骤==============================
    
    Add-Type -AssemblyName System.Linq;
    
    # ==========================当前的DbContext===========================
    # 获取现在 DbContext 内容
    $currentDbContext = Get-Content "./HoaDbContext.cs";
    
    # 将当前的所有using添加到数组中
    $currentUsingMatches=[regex]::Matches($currentDbContext,"usings+(?<namespace>.*?);");
    $currentUsingArray = New-Object -TypeName System.Collections.Generic.List[System.String];
    for($i=0;$i -le $currentUsingMatches.Count;$i++)
    {
      $namespace = $currentUsingMatches[$i].Value;
      if($namespace -ne $null){
        $currentUsingArray.Add($namespace);
      }
    }
    
    # 将当前的所有DbSet添加到数组中
    $currentDbSetMatches=[regex]::Matches($currentDbContext,"publics+virtuals+DbSet<(?<entity>.*?)>.*?}");
    $currentDbSetArray = New-Object -TypeName System.Collections.Generic.List[System.String];
    for($i=0;$i -le $currentDbSetMatches.Count;$i++)
    {
      $dbSet = $currentDbSetMatches[$i].Value;
      if($dbSet -ne $null){
        $currentDbSetArray.Add($dbSet);
      }
    }
    
    # 将当前的所有model声明添加到数组中
    $currentModelMatches=[regex]::Matches($currentDbContext,"modelBuilder.Entity<(?<entity>.*?)>.*?
    *
    *s*{([^}]*)});");
    $currentModelArray = New-Object -TypeName System.Collections.Generic.List[System.String];
    for($i=0;$i -le $currentModelMatches.Count;$i++)
    {
      $model = $currentModelMatches[$i].Value;
      if($model -ne $null){
        $currentModelArray.Add($model);
      }
    }
    
    # ==========================备份的DbContext===========================
    # 获取备份的 DbContext 内容
    $bakDbContext = Get-Content "./HoaDbContext.cs.20200713200720";
    
    # 将备份的所有using添加到数组中
    $bakUsingMatches=[regex]::Matches($bakDbContext,"usings+(?<namespace>.*?);");
    $bakUsingArray = New-Object -TypeName System.Collections.Generic.List[System.String];
    for($i=0;$i -le $bakUsingMatches.Count;$i++)
    {
      $namespace = $bakUsingMatches[$i].Value;
      if($namespace -ne $null){
        $bakUsingArray.Add($namespace);
      }
    }
    
    # 将备份的所有DbSet添加到数组中
    $bakDbSetMatches=[regex]::Matches($bakDbContext,"publics+virtuals+DbSet<(?<entity>.*?)>.*?}");
    $bakDbSetArray = New-Object -TypeName System.Collections.Generic.List[System.String];
    for($i=0;$i -le $bakDbSetMatches.Count;$i++)
    {
      $dbSet = $bakDbSetMatches[$i].Value;
      if($dbSet -ne $null){
        $bakDbSetArray.Add($dbSet);
      }
    }
    
    # 将备份的所有model声明添加到数组中
    $bakModelMatches=[regex]::Matches($bakDbContext,"modelBuilder.Entity<(?<entity>.*?)>.*?
    *
    *s*{([^}]*)});");
    $bakModelArray = New-Object -TypeName System.Collections.Generic.List[System.String];
    for($i=0;$i -le $bakModelMatches.Count;$i++)
    {
      $model = $bakModelMatches[$i].Value;
      if($model -ne $null){
          $bakModelArray.Add($model);
      }
    }
    
    # ==========================合并DbContext===========================
    
    # 合并去重后的using
    $usingArray=[System.Linq.Enumerable]::Union($currentUsingArray,$bakUsingArray);
    
    # 合并去重后的DbSet
    $dbSetArray=[System.Linq.Enumerable]::Union($currentDbSetArray,$bakDbSetArray);
    
    # 合并去重后的ModelBuilder
    $modelArray=[System.Linq.Enumerable]::Union($currentModelArray,$bakModelArray);
    
    $modelArray
    
    # 定义模板字符串
    $dbContextTemplate=@"
    @using
    
    namespace Hoa.EntityFrameworkCore
    {
        public partial class HoaDbContext : DbContext
        {
            public HoaDbContext()
            {
            }
    
            public HoaDbContext(DbContextOptions<HoaDbContext> options)
                : base(options)
            {
            }
    
            @dbSet
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                    optionsBuilder.UseSqlServer("Name=HoaDatabase");
                }
            }
    
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                @model
    
                modelBuilder.HasSequence("CountBy1", "Notes");
    
                modelBuilder.HasSequence("SEQ_POLICY_NUM_AM", "Policies")
                    .StartsAt(1056580)
                    .HasMin(0);
    
                modelBuilder.HasSequence("SEQ_POLICY_NUM_AS", "Policies")
                    .StartsAt(1004066)
                    .HasMin(0);
    
                modelBuilder.HasSequence("SEQ_POLICY_NUM_E5", "Policies")
                    .StartsAt(10000015)
                    .HasMin(0);
    
                modelBuilder.HasSequence("SEQ_POLICY_NUM_OTHER", "Policies")
                    .StartsAt(10000000000)
                    .HasMin(0);
    
                modelBuilder.HasSequence("SEQ_GROUP_NUM", "Reference").StartsAt(2750);
    
                OnModelCreatingPartial(modelBuilder);
            }
    
            partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
        }
    }
    "@;
    
    $dbContextTemplate=$dbContextTemplate.Replace("@using",[System.String]::Join("`n",$usingArray)).Replace("@dbSet",[System.String]::Join("`n",$dbSetArray).Replace("public","`t`tpublic")).Replace("@model",[System.String]::Join("`n",$modelArray).replace(";",";`n").replace("{","{`n").replace("}","}`n").Replace("modelBuilder.Entity","`t`t`tmodelBuilder.Entity").Replace(");",");`n")).Replace("                     .",".").Replace("                 .",".").Replace("             {","{");
    $dbContextTemplate | Out-File "./test.cs" ;
    
  • 相关阅读:
    用MFC(C++)实现拼音搜索
    MFC里ON_COMMAND_RANGE消息映射的ID问题
    01:MFC应用程序编程
    MFC笔记(DN)
    linuxmint系统定制与配置(2)-输入法
    linuxmint系统定制与配置(3)-字体
    linuxmint系统定制与配置(1)-系统初始配置
    笔记-读官方Git教程(2)~安装与配置
    笔记-读官方Git教程(1)~认识Git
    Python中多层List展平为一层
  • 原文地址:https://www.cnblogs.com/baiqian/p/13331668.html
Copyright © 2011-2022 走看看