CodeSimth的使用
我在开发MVC的时候,对于大众化的利用VS中自带的插件进行CodeFrist去生成Model和Mapping来说。 CodeSimth用起来是相对的方便,就是利用模板生成相应的代码,方便开发者快速生成代码,也就类似东软代码生成器一样。我们只需设计好模板,绑定数据库,代码就自动生成了。
CodeSimth的安装包网络链接:http://pan.baidu.com/s/1i52iYAp 其中包括了安装步骤文件。
那这款软件安装好后,如何使用呢?这个,真的很好用!一键生成,不像CodeFirst那么麻烦。
首先,我们可以在本地某个硬盘下新建一个文件夹并命名为MODELS,然后再在这个文件夹下新建2个文件夹分别命名为Model和Mapping。这是为了后期配置CodeSimth做准备。对于生成Model和Mapping,没有模板文件什么都是白搭。模板文件会根据数据库中的表结构生成对应的Model和Mapping。如果是新手的话,我还是不建议去使用的这款软件,不然就真的是个码农了。
模板一:BaseTemp.cst
<span style="font-size:18px;"><%@ Template Language="C#" TargetLanguage="Text" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="sourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True"
Optional="False" Category="数据库连接" %>
<%@ Property Name="NameSpace" Type="System.String" Category="Context" Default="mydb" Optional="true" %>
<%-- 注册实体层entity模板 --%>
<%@ Register Name="EntityTemplate" Template="EntityModel.cst" MergeProperties="False" ExcludeProperties="" %>
<%@ Register Name="EntityMapTemplate" Template="EntityMapping.cst" MergeProperties="False" ExcludeProperties="" %><script runat="template">
//解决方案输出路径
private string Directory = String.Empty;[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[Optional, NotChecked]
[DefaultValue("")]
public string OutputDirectory
{
get
{
return Directory;
}
set
{
if (value.EndsWith("\\")) value = value.Substring(0, value.Length -1);
Directory = value;
}
}private string DirectoryMapping = String.Empty;[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
[Optional, NotChecked]
[DefaultValue("")]
public string OutputDirectoryMapping
{
get
{
return DirectoryMapping;
}
set
{
if (value.EndsWith("\\")) value = value.Substring(0, value.Length -1);
DirectoryMapping = value;
}
}</script>
<script runat="template">
//生成实体Entity类
private void GenerateEntityClasses()
{
CodeTemplate Template =new EntityTemplate();
CodeTemplate MapTemplate = new EntityMapTemplate();Template.SetProperty("NameSpace", GetProperty("NameSpace"));MapTemplate.SetProperty("ProjectName", GetProperty("NameSpace"));foreach(TableSchema table in this.sourceDatabase.Tables)
{
string FileDirectory = OutputDirectory +"\\"+ table.Name +".cs";
//生成模板
Template.SetProperty("Table",table);
//文件输出
Template.RenderToFile(FileDirectory,true);
Debug.WriteLine(FileDirectory +" 创建成功.");
}
foreach(TableSchema table in this.sourceDatabase.Tables)
{
string FileDirectory = OutputDirectoryMapping +"\\"+ table.Name+"Map"+".cs";
//生成模板
MapTemplate.SetProperty("Table",table);
//文件输出
MapTemplate.RenderToFile(FileDirectory,true);
Debug.WriteLine(FileDirectory +" 创建成功.");
}}</script>
<%
//创建实体层Entity类
this.GenerateEntityClasses();Debug.WriteLine("OK");
%></span>
模板二:EfRepository.cst
<span style="font-size:18px;"><%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Debug="True" ResponseEncoding="UTF-8" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="NameSpace" Type="System.String" Category="Context" Default="mydb<span style="font-family: Arial, Helvetica, sans-serif;">" Optional="true" %></span>
<%@ Property Name="UsingSpace" Type="System.String" Category="Context" Default="mydb<span style="font-family: Arial, Helvetica, sans-serif;">" Optional="true" %></span>
<%@ Property Name="Table" Type="TableSchema" DeepLoad="True"Optional="False" Category="01. Getting Started - Required"Description="Database that the tables views, and stored procedures shouldbe based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, theEntire Database will then be generated." %>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using <%=UsingSpace%>.Core.Data;
using <%=UsingSpace%>.Core.Domain.Models;
using <%=UsingSpace%>.Data.Data;
using <%=UsingSpace%>.Data.Repositories.IRepositories;namespace <%=UsingSpace%>.Data.Repositories.EfRepositories
{public class <%=Table.Name%>Repository : EfRepository<<%=Table.Name%>>, I<%=Table.Name%>Repository{public AssetDetailRepository(IDbContext context): base(context){}}
}</span>
模板三:EntityMapping.cst
<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Debug="True" ResponseEncoding="UTF-8" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="ProjectName" Type="System.String" Category="Context" Default="mydb" Optional="true" %>
<%@ Property Name="Table" Type="TableSchema" DeepLoad="True"Optional="False" Category="数据库"Description="Database that the tables views, and stored procedures shouldbe based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, theEntire Database will then be generated." %>using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using <%=ProjectName%>.Core.Domain.Models;namespace <%=ProjectName%>.Data.Mapping
{public class <%=Table.Name%>Map : EntityTypeConfiguration<<%=Table.Name%>>{public <%=Table.Name%>Map(){// Primary Key<%-- this.HasKey(t => t.<%=Table.PrimaryKey %>);--%><%if(Table.HasPrimaryKey){foreach(ColumnSchema col in Table.PrimaryKey.MemberColumns){%>this.HasKey(t => t.<%=col.Name%>); <%} }%> // Properties<%foreach (ColumnSchema col in Table.Columns){%>//<%=col.Description %> this.Property(t => t.<%=col.Name %>)<%if(!col.AllowDBNull){%>.IsRequired() <% } %><%if(col.Size != 0 &&(col.DataType == DbType.AnsiString || col.DataType == DbType.AnsiStringFixedLength || col.DataType == DbType.String || col.DataType == DbType.StringFixedLength)) {%><%if(col.Size != -1){%>.HasMaxLength(<%=col.Size%>)<%} %> <%}%> <%if(col.DataType == DbType.Double || col.DataType == DbType.Decimal) {%>.HasPrecision(<%=col.Precision%>,<%=col.Scale%>)<%}%> .HasColumnName("<%=col.Name%>");<%} %>// Table & Column Mappingsthis.ToTable("<%=Table.Name%>");}}
}
模板四:EntityModel.cst
<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Debug="True" ResponseEncoding="UTF-8" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="NameSpace" Type="System.String" Category="Context" Default="mydb" Optional="true" %>
<%@ Property Name="Table" Type="TableSchema" DeepLoad="True"Optional="False" Category="01. Getting Started - Required"Description="Database that the tables views, and stored procedures shouldbe based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, theEntire Database will then be generated." %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace <%=NameSpace%>.Core.Domain.Models
{/// <summary>/// <%=Table.Description %>/// </summary>public partial class <%= Table.Name%>{
<%foreach(ColumnSchema col in Table.Columns){ %>/// <summary>/// <%=col.Description %>/// </summary>public <%=GetDataType(col) %> <%=col.Name %> { get;set; }
<% } %>}
}
<script runat="template">
public string GetDataType(ColumnSchema col){string result = "";switch(col.DataType){case DbType.AnsiString:case DbType.StringFixedLength:result = "string";break;case DbType.Guid:if(col.AllowDBNull){result = "Guid?";}else{result = "Guid";}break;case DbType.String:result = "string";break;case DbType.Binary:result = "byte[]";break;case DbType.Int16:case DbType.Int32:case DbType.Int64:if(col.AllowDBNull){result = "int?";}else{result = "int";}break;case DbType.DateTime:if(col.AllowDBNull){result = "DateTime?";}else{result = "DateTime";} break;case DbType.Boolean:if(col.AllowDBNull){result = "bool?";}else{result = "bool";} break;case DbType.Decimal:if(col.AllowDBNull){result = "decimal?";}else{result = "decimal";} break;case DbType.Double:if(col.AllowDBNull){result = "double?";}else{result = "double";} break;}return result;
}
</script>
模板五:IRepository.cst
<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Debug="True" ResponseEncoding="UTF-8" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Property Name="NameSpace" Type="System.String" Category="Context" Default="mydb" Optional="true" %>
<%@ Property Name="UsingSpace" Type="System.String" Category="Context" Default="mydb" Optional="true" %>
<%@ Property Name="Table" Type="TableSchema" DeepLoad="True"Optional="False" Category="01. Getting Started - Required"Description="Database that the tables views, and stored procedures shouldbe based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, theEntire Database will then be generated." %>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using <%=UsingSpace%>.Core.Data;
using <%=UsingSpace%>.Core.Domain.Models;namespace <%=UsingSpace%>.Data.Repositories.IRepositories
{public interface I<%=Table.Name%>Repository : IRepository<<%=Table.Name%>>{}
}
然后把五个模板文件拷到CodeSimth软件下Template Explorer下的My Templates文件夹下,然后为设置连接到某个本地数据库(仅包含架构)。为其配置model生成指向之前新建的文件夹Model的路径,把mapping生成指向之前新建的文件夹Mapping的路径。然后Click Generate按钮,一键生成Model和Mapping,然后把对应生成的model和Mapping拷到对应的文件下。(其中如果利用Pd去生成数据库架构的话,还可以其中编写Comments 注释,之后代码中也会生成对应的注释,方便后期开发的维护。)
这就大大减轻了工作量。
如果要用CodeSimth去生成Model和Mapping,我们也要安装另外两个软件 PD(PowerDesigner)和SqlSever数据库管理工具(应该也可以使用navicat这个软件)。Pd是用来设计数据库的就是设计数据库的架构,SqlSever这个软件用来管理数据库的数据的。这两个软件之后会再写文章,介绍一下。
CodeSimth的使用相关推荐
- 通过代码生成机制实现强类型编程-CodeSimth版
一直想写一个Code生成系列,但写到CodeSimth,发觉在TerryLee 和努力学习的小熊 两位大牛的博客里讲很详尽,所以就像写些示例方面的,但是苦于没有想到写些什么.最近Artech写了两篇从 ...
- Dbml文件提取建表TSql-CodeSmith
在昨天一个大学师弟,他问我能不能将LinqToSql文件转化为创建表的TSql语句,他是刚开始学习.NET,所以在网上下些示例看,但苦于没有数据库.所以就有了这一篇博客,作为我的Code生成技术的Co ...
- CodeSmith将模板文件批量生成文件的方法
以前写codeSmith模板的时候,如果直接像asp一样混排编写的话,代码输出来codeSmith中,不知道怎么保存到文件,后来把所有要输出的模板文字都用stringBuiler串接起来然后IO到文件 ...
- CodeDom六--实体类生成示例
CodeDom这个东西个人觉得知识点不多,前几个续节写的已差不多了.在这节将演示一个CodeDom示例: 数据库实体类的生成.这里先声明在如今的CodeSmith或者是T4模板中实现这些都很简单,并且 ...
- CodeSmith基础教程
请大家耐心看完所有的基础文章,前两篇网上发表的比较多,是CodeSmith英文帮助文档的第一篇,我后面写的基础是将其他所有的英文帮助全部翻译出来了,全部为本人手写翻译,希望对大家有所帮助 一.第一个模 ...
最新文章
- 深入浅出JVM的锁优化案例
- SAP Fiori application do filtering will real delete note in DB
- C和指针之字符串之strlen、strcpy、 strcat、strcmp使用总结
- 【前端知识学习】HTML5 学习笔记
- python excel 数据匹配实现vlookup功能_如何用python实现excel中的vlookup功能?
- HDU 2242 考研路茫茫——空调教室
- OpenCV-Mat笔记
- linux 网络 路由,网络路由的顺序在Linux中是否重要?
- echarts图表使用v-show控制图表显示不全的问题
- Clojure 学习入门(3)- 数字类型
- 微信小程序实现下拉刷新
- JavaScript中函数式编程的原理
- 面试官问:Java 中的锁有哪些?我跪了……
- 算法学习之路|反转链表
- 高薪设计师必修课 AE移动UI动效设计从入门到实战
- 双曲函数奇偶性_[快乐数学]双曲函数(二)
- java实现即时通讯软件
- windows10 wifi热点手机连接显示无网络连接问题解决
- JQuery实现shift键多选
- 在java中重写方法应遵循规则的包括_蘑菇街2017校园招聘笔试题
热门文章
- java 图片加密
- 用JS实现:图片压缩、图片加密
- 部分Up主精心推荐电脑软件链接
- Android8.0安装apk报错:Package xxx is currently frozen
- 【产品经理必备文档】述职报告/年终总结汇报ppt模板
- encodeURI,encodeURIComponent有什么区别?
- php 仿 js encodeURI
- 《机器学习》西瓜书课后习题作业笔记
- 含文档+PPT+源码等]精品微信小程序慢性疾病+后台管理系统|前后分离VUE[包运行成功]
- 直播平台源码实现状态栏滑动隐藏和tabbar的教程