PowerShell 自动合并 db first 的dbcontext

# ==============================实现步骤==============================
# 第一步,获取Using,组装并去重
# using\s+(?<namespace>.*?);

# 第二步,获取所有DbSet,组装并去重
# public\s+virtual\s+DbSet<(?<entity>.*?)>.*?\}

# 第三步,获取所有的Entity声明,组装并去重
# modelBuilder\.Entity<(?<entity>.*)>.*\r*\n*\s*\{([^}]*)\}\);

# ==============================实现步骤==============================

Add-Type -AssemblyName System.Linq;

# ==========================当前的DbContext===========================
# 获取现在 DbContext 内容
$currentDbContext = Get-Content "./HoaDbContext.cs";

# 将当前的所有using添加到数组中
$currentUsingMatches=[regex]::Matches($currentDbContext,"using\s+(?<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,"public\s+virtual\s+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>.*?)>.*?\r*\n*\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,"using\s+(?<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,"public\s+virtual\s+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>.*?)>.*?\r*\n*\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" ;
posted @ 2020-07-17 17:57  百小僧  阅读(183)  评论(0编辑  收藏  举报