首页 技术 正文
技术 2022年11月15日
0 收藏 623 点赞 4,868 浏览 2543 个字

Program.cs

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;namespace Migration
{
class Program
{
static void Main(string[] args)
{
string contextFileFullPath = @"D:\\Projects\Context\Context.cs";
string contextContent = File.ReadAllText(contextFileFullPath, Encoding.UTF8);
string savedParentFoldFullPath = @"D:\Mapping"; string regexFindText = @"\s{12}modelBuilder\.Entity\<(?<entityName>\w+)\>\(entity\s*\=\>\S*\s*\{(?<innerCode>[\s\S]*?)\s{13}?\}\)\;"; if (Regex.IsMatch(contextContent, regexFindText))
{
Match matchItem = Regex.Match(contextContent, regexFindText, RegexOptions.Multiline | RegexOptions.IgnoreCase); while (matchItem.Success)
{
string entityName = matchItem.Groups["entityName"].Value;
string innerCode = matchItem.Groups["innerCode"].Value;
WriteToFile(entityName, innerCode, savedParentFoldFullPath);
matchItem = matchItem.NextMatch();
}
Console.WriteLine("The regular expressions are correct, perfect!");
}
else
{
Console.WriteLine("The regular expression is incorrect, please modify!");
}
Console.ReadLine(); } static void WriteToFile(string entityName, string innerCode, string savedParentFoldFullPath)
{
string templateRelativePath = "CodeTemplate.txt";
string templateFullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, templateRelativePath);
string templateContent = File.ReadAllText(templateFullPath, Encoding.UTF8); if (!Directory.Exists(savedParentFoldFullPath))
{
try
{
Directory.CreateDirectory(savedParentFoldFullPath);
}
catch (Exception ex)
{
Console.WriteLine("目录 {0} 创建失败!", ex.Message);
return;
}
} string fileNameWithoutPath = string.Format("{0}Map.cs", entityName);
string savedSingleFileFullPath = Path.Combine(savedParentFoldFullPath, fileNameWithoutPath); string singleFileContent = templateContent
.Replace("{#entityName#}", entityName)
.Replace("{#innerCode#}", innerCode); File.WriteAllText(savedSingleFileFullPath, singleFileContent, Encoding.UTF8);
}
}
}

.csproj

<Project Sdk="Microsoft.NET.Sdk">  <PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup> <ItemGroup>
<None Update="CodeTemplate.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup></Project>

CodeTemplate.txt

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using XXX.Domain.Core;
using XXX.Ef.Mapping;namespace XXX.Data.Ef.Mapping
{
public class {#entityName#}Map : EntityTypeConfiguration<{#entityName#}>
{
#region Methods /// <summary>
/// Configures the entity
/// </summary>
/// <param name="builder">The builder to be used to configure the entity</param>
public override void Configure(EntityTypeBuilder<{#entityName#}> entity)
{
{#innerCode#}
} #endregion
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850