如何自动生成 Entity Framework 的 Mapping 文件?
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 文件?的更多相关文章
- mybatis generate 自动生成 entity dao 和 xml 文件
其中的一种方式 ,使用maven 插件 <build> <plugins> <plugin> <groupId>org.mybatis.generato ...
- 【Mybatis】使用Mybatis-Generator自动生成entity、dao、mapping
使用过mybatis的应该都有用过Mybatis-Generator,本文主要介绍使用Mybatis-Generator来自动生成entity.dao.mapping文件. Mybatis-Gener ...
- java自动生成entity文件
网上关于自动生成entity文件的代码很多,看了很多代码后,在先辈们的基础上再完善一些功能(指定多个表,全部表). 为了使用方便所以把两个类写在一个java文件中,所以大家可以直接拿这个java文件, ...
- 简单三步快速学会使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件(postgre使用实例)
前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...
- 简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)
前言: mybatis-generator是根据配置文件中我们配置的数据库连接参数自动连接到数据库并根据对应的数据库表自动的生成与之对应mapper映射(比如增删改查,选择性增删改查等等简单语句)文件 ...
- mybatis根据表逆向自动化生成代码(自动生成实体类、mapper文件、mapper.xml文件)
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- 使用mybatis-generator在自动生成Model类和Mapper文件
使用mybatis-generator插件可以很轻松的实现mybatis的逆向工程,即,能通过表结构自动生成对应的java类及mapper文件,可以大大提高工作效率,并且它提供了很多自定义的设置可以应 ...
- 记录一下idea自动生成Entity
最近在鼓捣spring -boot ,真好用,学习到jpa. 通过生成Entity 文件,能够快速的生成数据库,并且使用 JpaRepository 的基本增删查改 方法,好用的一批. 可是随之,问题 ...
- java web(七): mybatis的动态sql和mybatis generator自动生成pojo类和映射文件
前言: MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据 不同条件拼接 SQL 语句的痛苦.例如拼接时要确保不能忘记添加必要的空格,还 ...
随机推荐
- 用Java实现二叉查找树
二叉查找树的实现 1. 原理 二叉查找树,又称为二叉排序树.二叉搜索树.对于树中每一个节点X,它的左子树中所有项的值小于X中的项,而它的右子树中所有项的值大于X中的项.二叉查找树的平均深度为O(log ...
- 用 Python 带你看各国 GDP 变迁
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 周萝卜 源自:萝卜大杂烩 PS:如有需要Python学习资料的小伙伴 ...
- 获取给定地址中的域名,substring()截取
package seday01;/** * 获取给定地址中的域名 * @author xingsir */public class Test { public static void main(Str ...
- 使用百度的webuploader进行附件上传
相较于之前使用的上传空间的优点:支持html5,不用再安装flash插件,没有大小限制,分片以后上传,上传以后再进行合并. 前端js代码 <script type="text/java ...
- Python使用DB-API操作MySQL数据库
Python提供了一个标准数据库API,称为DB-API,用于处理基于SQL的数据库.与任何底层数据库的交互都可以使用DB-API,因为DB-API在代码与驱动程序之间提供了一个抽象层,可以根据需要替 ...
- 网络爬虫之使用pyppeteer替代selenium完美绕过webdriver检测
1引言 曾经使用模拟浏览器操作(selenium + webdriver)来写爬虫,但是稍微有点反爬的网站都会对selenium和webdriver进行识别,网站只需要在前端js添加一下判断脚本,很容 ...
- 分布式文件管理系统HDFS
Hadoop 分布式文件管理系统HDFS可以部署在廉价硬件之上,能够高容错. 可靠地存储海量数据(可以达到TB甚至PB级),它还可以和Yam中的MapReduce 编程模型很好地结合,为应用程序提供高 ...
- JDBC API浅析
使用java开发数据库应用程序一般都需要用到四个接口:Driver.Connection.Statement.ResultSet 1.Driver接口用于加载驱动程序 2.Connection接口用于 ...
- Java实现MapReduce Wordcount案例
先改pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...
- VMware workstation安装centos7,无ifconfig命令
一.centos7默认未启动ifconfig,vi /etc/sysconfig/network-scripts/ifcfg-ens33,改成ONBOOT=yes: systemctl restart ...