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
}
}

如何自动生成 Entity Framework 的 Mapping 文件?的更多相关文章

  1. mybatis generate 自动生成 entity dao 和 xml 文件

    其中的一种方式 ,使用maven 插件 <build> <plugins> <plugin> <groupId>org.mybatis.generato ...

  2. 【Mybatis】使用Mybatis-Generator自动生成entity、dao、mapping

    使用过mybatis的应该都有用过Mybatis-Generator,本文主要介绍使用Mybatis-Generator来自动生成entity.dao.mapping文件. Mybatis-Gener ...

  3. java自动生成entity文件

    网上关于自动生成entity文件的代码很多,看了很多代码后,在先辈们的基础上再完善一些功能(指定多个表,全部表). 为了使用方便所以把两个类写在一个java文件中,所以大家可以直接拿这个java文件, ...

  4. 简单三步快速学会使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件(postgre使用实例)

    前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...

  5. 简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)

    前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...

  6. mybatis根据表逆向自动化生成代码(自动生成实体类、mapper文件、mapper.xml文件)

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  7. 使用mybatis-generator在自动生成Model类和Mapper文件

    使用mybatis-generator插件可以很轻松的实现mybatis的逆向工程,即,能通过表结构自动生成对应的java类及mapper文件,可以大大提高工作效率,并且它提供了很多自定义的设置可以应 ...

  8. 记录一下idea自动生成Entity

    最近在鼓捣spring -boot ,真好用,学习到jpa. 通过生成Entity 文件,能够快速的生成数据库,并且使用 JpaRepository 的基本增删查改 方法,好用的一批. 可是随之,问题 ...

  9. java web(七): mybatis的动态sql和mybatis generator自动生成pojo类和映射文件

    前言: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据 不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还 ...

随机推荐

  1. 《DSLR-Quality Photos on Mobile Devices with Deep Convolutional Networks》研读笔记

    <DSLR-Quality Photos on Mobile Devices with Deep Convolutional Networks>研读笔记 论文标题:DSLR-Quality ...

  2. Java开发桌面程序学习(一)——JavaFx+Jfoenix初始以及搭建

    Java开发桌面程序学习(一)--JavaFx+Jfoenix初始以及搭建 前言 想做一个Java的桌面程序,但是,使用原生的Swing感觉又十分麻烦,那个布局都是拿代码设置,看着十分的乱,偶然的情况 ...

  3. MySQL(8)---游标

    Mysql(8)-游标 上一遍博客写了有关存储过程的语法知识 Mysql(7)---存储过程 游标或许你在工作中很少用到,但用不到不代表不去了解它,但你真正需要它来解决问题的时候,再花时间去学习很可能 ...

  4. Go template高级用法、深入详解、手册、指南、剖析

    入门示例 以下为test.html文件的内容,里面使用了一个template语法{{.}}. <!DOCTYPE html> <html> <head> <m ...

  5. SQL server已经设置为单用户模式,还是无法做分离、属性设置等操作

    https://www.cnblogs.com/xingyunqiu/p/10336938.html SQL server已经设置为单用户模式,Sql server还原失败数据库正在使用,无法获得对数 ...

  6. media适配css

    /*引入适配的less*/ html { font-size: 16px; } @media only screen and (min-width: 320px) { html { font-size ...

  7. react聊天室|react+redux仿微信聊天IM实例|react仿微信界面

    一.项目概况 基于react+react-dom+react-router-dom+redux+react-redux+webpack2.0+react-photoswipe+swiper等技术混合开 ...

  8. swift(四)swift的广义匹配

    //swift的广义匹配 let x = switch x { ...: println("个位数") ...: println("十位数") default: ...

  9. CentOS服务器apache绑定多个域名的方法

    这篇文章主要为大家详细介绍了CentOS服务器apache绑定多个域名的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 Apache是最流行的HTTP服务器软件之一,其以快速.可靠(稳定) ...

  10. 如何将MagicaVoxel模型导入UE4中(1)

    前言 当初在选择自己项目的美术风格时,由于自己的美术基础实在是太差,所以选择了体素风格来构建(其实还是MagicaVoxel的建模操作很容易上手),但是将自己千辛万苦做好的模型导入至项目中时,出现了这 ...