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" ;
    
  • 相关阅读:
    C++11 并发指南三(Lock 详解)
    C++11 并发指南六(atomic 类型详解四 C 风格原子操作介绍)
    C++11 并发指南六(atomic 类型详解三 std::atomic (续))
    C++11 并发指南六( <atomic> 类型详解二 std::atomic )
    C++11 并发指南六(atomic 类型详解一 atomic_flag 介绍)
    C++11 并发指南五(std::condition_variable 详解)
    腾讯地图定位及坐标解析
    控制器里把文件转为二进制输出下载
    C#操作mongodb简记
    MYSQL备份还原问题
  • 原文地址:https://www.cnblogs.com/baiqian/p/13331668.html
Copyright © 2011-2022 走看看