如何自动生成 Entity Framework 的 Mapping 文件?
2019-09-25 18:10 音乐让我说 阅读(323) 评论(0) 编辑 收藏 举报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 } }
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步