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. python免密远程执行shell

    使用paramiko库:https://github.com/paramiko/paramiko 简单封装SSH类 import paramiko class SSH: def __init__(se ...

  2. 在.net core程序中使用EntityFrameok(非EF Core)

    最近用NoSQL较多写,用传统的EF到不多,但在一些.net core小程序中也小试牛刀过,不过当时用的是微软为.net core量身定制的Entity Framework Core,只是一些比较常规 ...

  3. Linux网络——修改配置文件

    Linux网络——修改配置文件 摘要:本文主要学习了如何通过修改配置文件来设置网络参数. 配置文件 通过修改系统的配置文件为系统设置网络参数,这种方式的优点是可以永久保存,计算机重启后仍然生效.缺点是 ...

  4. 怎么在CAD中测量图纸距离?来看看这两种方法

    在CAD中设计图纸最重要的就是图纸的尺寸,俗话说也就是图纸间的距离.通过正确的数据设计出的图纸才能够准确,也能够避免施工时事不必要的误差.那怎么在CAD中测量图纸距离呢?具体要怎么来进行操作呢?下面我 ...

  5. MYSQL 复制整个数据库

    同一个服务器 mysqldump old -u root -p123456  | mysql new -u root -p123456 不同服务器(在源数据库服务器上执行) mysqldump old ...

  6. Dynamics 365 登录报错:MSIS7042

    微软动态CRM专家罗勇 ,回复329或者20190504可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! 今天访问我的CRM环境报错,AD FS登录页面输入用户名和密码登 ...

  7. PeriscopeHeartAnimation

    // // ViewController.m // PeriscopeHeartAnimation // // Created by ldj on 4/28/15. // Copyright (c) ...

  8. python生产者和消费者模式实现(三)进程池方式

    注意:如果要使用Pool(进程池方式)创建进程,就需要使用multiprocessing.Manager()中的 Queue(),而不是multiprocessing.Queue() import t ...

  9. C++学习03_对象

    1.类 类决定了一个对象具有什么样的属性功能. class MyFirstClass { }; //跟C++结构情况相似 //类的第一个字母采用大写是一种习惯的上的标准. //与C++结构情况类似 类 ...

  10. itest(爱测试) 4.1.1 发布,开源BUG 跟踪管理 & 敏捷测试管理软件

    v4.1.1下载地址 :itest下载 itest 简介:查看简介 itest 开源敏捷测试管理,testOps 践行者.可按测试包分配测试用例执行,也可建测试迭代(含任务,测试包,BUG)来组织测试 ...