用我的MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many)...

MyGeneration的几个NHibernate模板功能已经很强,但还是存在些问题。例如:Guid主键支持不佳,代码不易修改,不支持中文注释等等。所以我决定自己来改写此模版。我把一部分通用的函数提取到自己定义的基类中,这样调试和修改都方便另外增加了一部分新功能。

NHibernate里面的关系写起来也很烦人,很容易出错,所以我写了另一个模版专门生成关系代码。只需要把生成的代码拷到映射类文件和.hbm.xml文件中就可以了。

下载

如果你兴趣自己写模版,或者使用中有问题可以查看强大的代码生成工具MyGeneration

使用中发现什么问题,或者是有什么好的意见建议请及时和我联系。十分感谢!

下载完成后把DDLLY.MyGenerationTemplate.dll拷贝到MyGeneration的安装路径。把模版文件拷贝到MyGeneration的安装路径下的Templates目录里面的NHibernate目录里。

生成映射文件

运行MyGeneration,选择Edit里面的Default Settings...,进行适当的配置。如图

在Template Browser里面的NHibernate找到"DDL NHibernate Object Mapping"。运行此模版

输出路径表示生成模版的生成路径。启用nullable类型表示在.Net2.0中使用nullable类型,如果。

提示:你可以按住Ctrl或者Shift选择多个表。

Save按钮可以把的你设置存储在注册表中,下次将自动获得保存的设置。

选中你需要生成映射类的表,点Ok按钮。将生成映射文件。你可以在输出路径中找到他们。

下面是我生成的文件

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
InBlock.gif/*NHibernate映射代码模板
InBlock.gif/*作者:DDL
InBlock.gif/*版本更新和支持:
http://renrenqq.cnblogs.com/
InBlock.gif/*日期:2006年8月14日 
ExpandedBlockEnd.gif
*/

None.gif
using  System;
None.gif
None.gif
namespace  MyNamePlace
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///    
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [Serializable]
InBlock.gif    
public sealed class User
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
私有成员#region 私有成员
InBlock.gif            
InBlock.gif        
private bool m_IsChanged;
InBlock.gif        
private bool m_IsDeleted;
InBlock.gif        
private int m_UserId; 
InBlock.gif        
private string m_UserName; 
InBlock.gif        
private string m_Password; 
InBlock.gif        
private string m_Email;         
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
默认( 空 ) 构造函数#region 默认( 空 ) 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 默认构造函数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public User()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_UserId 
= 0
InBlock.gif            m_UserName 
= null
InBlock.gif            m_Password 
= null
InBlock.gif            m_Email 
= null
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
公有属性#region 公有属性
InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>        

InBlock.gif        public  int UserId
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_UserId; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ m_IsChanged |= (m_UserId != value); m_UserId = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>        

InBlock.gif        public  string UserName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_UserName; }
InBlock.gif            
set    
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( value != null)
InBlock.gif                    
if( value.Length > 64)
InBlock.gif                        
throw new ArgumentOutOfRangeException("Invalid value for UserName", value, value.ToString());
InBlock.gif                
InBlock.gif                m_IsChanged 
|= (m_UserName != value); m_UserName = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>        

InBlock.gif        public  string Password
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_Password; }
InBlock.gif            
set    
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( value != null)
InBlock.gif                    
if( value.Length > 32)
InBlock.gif                        
throw new ArgumentOutOfRangeException("Invalid value for Password", value, value.ToString());
InBlock.gif                
InBlock.gif                m_IsChanged 
|= (m_Password != value); m_Password = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 
ExpandedSubBlockEnd.gif        
/// </summary>        

InBlock.gif        public  string Email
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_Email; }
InBlock.gif            
set    
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( value != null)
InBlock.gif                    
if( value.Length > 64)
InBlock.gif                        
throw new ArgumentOutOfRangeException("Invalid value for Email", value, value.ToString());
InBlock.gif                
InBlock.gif                m_IsChanged 
|= (m_Email != value); m_Email = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif            
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对象的值是否被改变
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool IsChanged
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_IsChanged; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 对象是否已经被删除
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool IsDeleted
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn m_IsDeleted; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion
 
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
公有函数#region 公有函数
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 标记对象已删除
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void MarkAsDeleted()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_IsDeleted 
= true;
InBlock.gif            m_IsChanged 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
重写Equals和HashCode#region 重写Equals和HashCode
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用唯一值实现Equals
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override bool Equals( object obj )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
ifthis == obj ) return true;
InBlock.gif            
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
InBlock.gif            User castObj 
= (User)obj; 
InBlock.gif            
return ( castObj != null ) &&
InBlock.gif                ( 
this.m_UserId == castObj.UserId );
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用唯一值实现GetHashCode
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override int GetHashCode()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
int hash = 57
InBlock.gif            hash 
= 27 * hash * m_UserId.GetHashCode();
InBlock.gif            
return hash; 
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

 

None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.0" >
None.gif    
< class  name ="MyNamePlace.User,MyAssembly"  table ="T_User" >
None.gif
None.gif        
< id  name ="UserId"  column ="UserId"  type ="Int32"  unsaved-value ="0" >
None.gif            
< generator  class ="native" />
None.gif        
</ id >
None.gif        
< property  column ="UserName"  type ="String"  name ="UserName"  not-null ="true"  length ="64"   />
None.gif        
< property  column ="Password"  type ="String"  name ="Password"  not-null ="true"  length ="32"   />
None.gif        
< property  column ="Email"  type ="String"  name ="Email"  length ="64"   />
None.gif        
None.gif    
</ class >
None.gif
</ hibernate-mapping >
None.gif

备注:
        我的数据库表名以"T_"开头,生成类时我会用"_"后面的内容。例如:T_Parent对应的类是Parent。如果你的表名中没有"_"。将会取表名做类名。
  如果你在设计数据表时把字段的描述加上,生成的代码文件的注释中将会有属性的描述。

注意:在把他们加入Visual Studio后别忘了设置为嵌入的资源。

生成关系

在Template Browser里面的NHibernate找到"DDL NHibernate Relation Mapping"。运行此模版

选择你需要的表,需要的关系。点OK。

我们以双向的one-to-many为例

你可以看到下面的生成代码。

None.gif // Parent
None.gif
< bag name = " Childs "  cascade = " all "  lazy = " true "  inverse = " true " >
None.gif
< key column = " ParentId " ></ key >
None.gif
< one - to - many  class = " MyNamePlace.Child,MyAssembly " ></ one - to - many >
None.gif
</ bag >
None.gif
None.gif
private  IList m_Child = new  ArrayList();
None.gif
None.gif
public  IList Childs
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
getdot.gif{return m_Child;}
ExpandedSubBlockStart.gifContractedSubBlock.gif
setdot.gif{m_Child=value;}
ExpandedBlockEnd.gif}

None.gif
None.gif
// Child
None.gif
< many - to - one name = " Parent "  column = " ParentId "  
None.gif
class = " MyNamePlace.Parent,MyAssembly "   />
None.gif
None.gif
private  Parent m_Parent;
None.gif
None.gif
public  Parent Parent
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif
getdot.gif{return m_Parent;}
ExpandedSubBlockStart.gifContractedSubBlock.gif
setdot.gif{m_Parent=value;}
ExpandedBlockEnd.gif}

None.gif

把他们拷贝到你生成的模版文件里面。“//Parent”后面的拷贝到Parent的映射类文件和.hbm.xml文件中。“//Child”后面的拷贝到Child的映射类文件和.hbm.xml文件中。

注意:需要把Child类和.hbm.xml里的ParentId去掉,不然会出现两个属性映射到一个字段的错误。

DDL NHibernate Relation Mapping模板使用注意:

    保持主外键的名称一致,比如T_Parent中主键名为ParetId,T_Child中与其参照的外键名也为ParentId。

    主表先选择,Parent-Child关系中先选择Parent,Person-Employee关系中先选择Person。

    多对多关系才会用到中间表下拉框。


其他的关系生成操作方法类似,我不再复述。 

如果你对关联不是很熟悉请参见NHibernate的关联映射(one-to-one,one-to-many,many-to-many)以及cascade分析。本模版生成的文件都使用NHibernate的关联映射(one-to-one,one-to-many,many-to-many)以及cascade分析一文的典型设置。可以满足绝大部分的情况。当然你也可以适当修改后使用。


模版更新:

    增加了对Guid主键的支持,中文的注释,其他代码改善,部分代码提取到自定义的基类。

     2006.8.24 修改部分bug



很多人反应模版使用问题,鉴于我这里没有.net1.0或者1.1的环境,所以把代码给出来,可以自己编译一个dll。

ContractedBlock.gif ExpandedBlockStart.gif DDLNHibernateDotNetScriptTemplate
None.gifusing System;
None.gif
using System.Globalization;
None.gif
using System.Text.RegularExpressions;
None.gif
using Dnp.Utils;
None.gif
using MyMeta;
None.gif
using Zeus;
None.gif
using Zeus.DotNetScript;
None.gif
using Zeus.UserInterface;
None.gif
None.gif
namespace DDLLY.MyGenerationTemplate
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public abstract class DDLNHibernateDotNetScriptTemplate : _DotNetScriptTemplate
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected GuiController ui;
InBlock.gif        
protected dbRoot MyMeta;
InBlock.gif        
protected Utils DnpUtils;
InBlock.gif
InBlock.gif        
public DDLNHibernateDotNetScriptTemplate(IZeusContext context)
InBlock.gif            : 
base(context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ui 
= context.Objects["ui"as GuiController;
InBlock.gif            MyMeta 
= context.Objects["MyMeta"as dbRoot;
InBlock.gif            DnpUtils 
= context.Objects["DnpUtils"as Utils;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 前缀
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected string _prefix;
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建XML
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected bool _CreatingXML = false;
InBlock.gif
InBlock.gif        
protected string _NullSufix;
InBlock.gif
InBlock.gif        
InBlock.gif        
protected string ConvertNHibernate(string Type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string retVal = Type;
InBlock.gif
InBlock.gif            
switch (Type)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "bool":
InBlock.gif                    retVal 
= "Boolean";
InBlock.gif                    
break;
InBlock.gif                
case "byte":
InBlock.gif                    retVal 
= "Byte";
InBlock.gif                    
break;
InBlock.gif                
case "sbyte":
InBlock.gif                    retVal 
= "SByte";
InBlock.gif                    
break;
InBlock.gif                
case "char":
InBlock.gif                    retVal 
= "Char";
InBlock.gif                    
break;
InBlock.gif                
case "decimal":
InBlock.gif                    retVal 
= "Decimal";
InBlock.gif                    
break;
InBlock.gif                
case "double":
InBlock.gif                    retVal 
= "Double";
InBlock.gif                    
break;
InBlock.gif                
case "float":
InBlock.gif                    retVal 
= "Single";
InBlock.gif                    
break;
InBlock.gif                
case "int":
InBlock.gif                    retVal 
= "Int32";
InBlock.gif                    
break;
InBlock.gif                
case "uint":
InBlock.gif                    retVal 
= "UInt32";
InBlock.gif                    
break;
InBlock.gif                
case "long":
InBlock.gif                    retVal 
= "Int64";
InBlock.gif                    
break;
InBlock.gif                
case "ulong":
InBlock.gif                    retVal 
= "UInt64";
InBlock.gif                    
break;
InBlock.gif                
case "short":
InBlock.gif                    retVal 
= "Int16";
InBlock.gif                    
break;
InBlock.gif                
case "ushort":
InBlock.gif                    retVal 
= "UInt16";
InBlock.gif                    
break;
InBlock.gif                
case "string":
InBlock.gif                    retVal 
= "String";
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return retVal;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//protected string ColumnToMemberVariable(IColumn Column)
InBlock.gif        
//{
InBlock.gif        
//    return _prefix + UniqueColumn(Column).ToLower();
InBlock.gif        
//}
InBlock.gif
InBlock.gif        
//protected string ColumnToPropertyName(IColumn Column)
InBlock.gif        
//{
InBlock.gif        
//    return ToPascalCase(UniqueColumn(Column));
InBlock.gif        
//}
InBlock.gif
InBlock.gif        
//protected string ColumnToArgumentName(IColumn Column)
InBlock.gif        
//{
InBlock.gif        
//    return UniqueColumn(Column).ToLower();
InBlock.gif        
//}
InBlock.gif
InBlock.gif        
//protected string ColumnToNHibernateProperty(IColumn Column)
InBlock.gif        
//{
InBlock.gif        
//    return _prefix + UniqueColumn(Column);
InBlock.gif        
//}
InBlock.gif
InBlock.gif
InBlock.gif        
// nhibernate doesn't have these, so use the existing types
InBlock.gif
        protected string ColumnToNHibernateType(IColumn Column)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string retVal = Column.LanguageType;
InBlock.gif
InBlock.gif            
switch (retVal)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
case "sbyte":
InBlock.gif                    retVal 
= "byte";
InBlock.gif                    
break;
InBlock.gif                
case "uint":
InBlock.gif                    retVal 
= "int";
InBlock.gif                    
break;
InBlock.gif                
case "ulong":
InBlock.gif                    retVal 
= "long";
InBlock.gif                    
break;
InBlock.gif                
case "ushort":
InBlock.gif                    retVal 
= "short";
InBlock.gif                    
break;
InBlock.gif                
case "bool":
InBlock.gif                
case "decimal":
InBlock.gif                
case "float":
InBlock.gif                
case "byte":
InBlock.gif                
case "short":
InBlock.gif                
case "int":
InBlock.gif                
case "long":
InBlock.gif                    
if (!_CreatingXML)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (Column.IsNullable)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            retVal 
= retVal + _NullSufix;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return retVal;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////// <summary>
InBlock.gif        
///// 首字母大写
InBlock.gif        
///// </summary>
InBlock.gif        
///// <param name="name"></param>
ExpandedSubBlockEnd.gif        
///// <returns></returns>

InBlock.gif        //protected string ToLeadingCaps(string name)
InBlock.gif        
//{
InBlock.gif        
//    char[] chars = name.ToLower().ToCharArray();
InBlock.gif        
//    chars[0] = Char.ToUpper(chars[0]);
InBlock.gif        
//    return new string(chars);
InBlock.gif        
//}
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 首字母小写
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="name"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private string ToLeadingLower(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
char[] chars = name.ToCharArray();
InBlock.gif            chars[
0= Char.ToLower(chars[0]);
InBlock.gif            
return new string(chars);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 私有成员名
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="name"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected string ToVariableName(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return _prefix + name;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 参数名
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="name"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected string ToArgumentName(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return ToLeadingLower(name);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////// <summary>
InBlock.gif        
///// 统计必需(不允许空)的的字段个数
InBlock.gif        
///// </summary>
InBlock.gif        
///// <param name="Columns"></param>
ExpandedSubBlockEnd.gif        
///// <returns></returns>

InBlock.gif        //protected int CountRequiredFields(IColumns Columns)
InBlock.gif        
//{
InBlock.gif        
//    return Columns.Count - CountNullableFields(Columns);
InBlock.gif        
//}
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**////// <summary>
InBlock.gif        
///// 统计允许为空的字段个数
InBlock.gif        
///// </summary>
InBlock.gif        
///// <param name="Columns"></param>
ExpandedSubBlockEnd.gif        
///// <returns></returns>

InBlock.gif        //protected int CountNullableFields(IColumns Columns)
InBlock.gif        
//{
InBlock.gif        
//    int i = 0;
InBlock.gif        
//    foreach (IColumn c in Columns)
InBlock.gif        
//    {
InBlock.gif        
//        if (c.IsNullable)
InBlock.gif        
//        {
InBlock.gif        
//            i++;
InBlock.gif        
//        }
InBlock.gif        
//    }
InBlock.gif        
//    return i;
InBlock.gif        
//}
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 统计唯一字段的个数(非空且为主键)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Columns"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected int CountUniqueFields(IColumns Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int i = 0;
InBlock.gif            
foreach (IColumn c in Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!c.IsNullable && c.IsInPrimaryKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    i
++;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return i;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据IColumn获得不同的Generator
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="Column"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected string GetGeneratorString(IColumn Column)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
if (Column.DataTypeName == "uniqueidentifier")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "guid";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
if(Column.IsAutoKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "native";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return "assigned";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 表名转换为类名
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tableName"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected string TableNameToClassName(string tableName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int index = tableName.LastIndexOf("_");
InBlock.gif            
return tableName.Substring(index + 1);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

需要引用以下类库(都在MyGeneration的安装路径里):
Dnp.Utils.dll
DotNetScriptingEngine.dll
MyMeta.dll
PluginInterfaces.dll
Zeus.dll

转载于:https://www.cnblogs.com/renrenqq/archive/2006/08/23/484216.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值