下面是接口层代码:codesimit模板<%@ CodeTemplate  Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Design" %>
<%@ Assembly Name="System.DirectoryServices" %>
<%@ Assembly Name="System.Web" %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import NameSpace="System.IO" %>
<%@ Import NameSpace="System.Text" %>
<%@ Import NameSpace="System.Text.RegularExpressions" %>
<%@ Import NameSpace="System.Diagnostics" %>
<%@ Import NameSpace="System.Xml" %>
<%@ Import NameSpace="System.Xml.Xsl" %>
<%@ Import NameSpace="System.Xml.XPath" %> 
<%-- 1. Datasource --%>
<%@ Property Name="CurentTable" Type="SchemaExplorer.TableSchema" DeepLoad="True" Optional="False" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OA.Entity;
namespace OA.IDAL
{
   public interface  IDAL<%=this.CurentTable.Name%>
    {
       int Insert(Entity.<%=this.CurentTable.Name%> obj);
       int Update(Entity.<%=this.CurentTable.Name%> obj);
       int Delete(Entity.<%=this.CurentTable.Name%> obj);
       List<Entity.<%=this.CurentTable.Name%>> LoadAll();
       Entity.<%=this.CurentTable.Name%> LoadById(<%=PKArgsForTable()%>);
////////////////////////////主表找子表
<% foreach(TableKeySchema pk in this.CurentTable.ForeignKeys) 
{%>
       List<Entity.<%=this.CurentTable.Name%>> QuerryList<%=pk.Name%>(<%=this.主键在子表中的外键所包含的列的参数列表带数据类型(pk)%>);
      <%}%> 
   
      } 
}      <script runat="template">
    
    //方法:根据表生成所有列构成的参数列表
    public string AllArgumentsForTable() 
    {
     string args="";
    foreach(ColumnSchema col in this.CurentTable.Columns)
    {
        args+=string.Format("{0} {1},",col.DataType,col.Name);
    }
    return args.Substring(0,args.Length-1);
    }
    
    //生成实体类属性赋值 根据所有列生成 列名=参数名,
    public string AllPropertySetValueForTable()
    {
     string args="";
    foreach(ColumnSchema col in this.CurentTable.Columns)
    {
        args+=string.Format("{0}={1},",col.Name,col.Name);
    }
   return args.Substring(0,args.Length-1);
    }
    
    //方法:生成列的主键列构成的参数列表
    public string PKArgsForTable()
    {    string args="";
    //foreach(ColumnSchema col in this.CurentTable.Columns)
    //{
      //  if(col.IsPrimaryKeyMember)
        //{
        //args+=string.Format("{0} {1},",col.Name,col.Name);
        //} 
    //}
    
    foreach (MemberColumnSchema pkCol in this.CurentTable.PrimaryKey.MemberColumns)
    {
       args+=string.Format("{0} {1},",pkCol.Column.DataType, pkCol.Column.Name);
    }
    return args.Substring(0,args.Length-1);
   }
//方法:生成列的主键列的赋值
    public string PKArgsSetValueForTable()
    {    string args="";
    //foreach(ColumnSchema col in this.CurentTable.Columns)
    //{
      //  if(col.IsPrimaryKeyMember)
        //{
        //args+=string.Format("{0}={1},",col.Name,col.Name);
        //} 
    //}
    
    foreach (MemberColumnSchema pkCol in this.CurentTable.PrimaryKey.MemberColumns)
    {
       args+=string.Format("{0}={1},",pkCol.Column.Name, pkCol.Column.Name);
    }
   return args.Substring(0,args.Length-1);
   }//方法:生成主键列名字
    public string PKArgsWithoutDataTypeForTable()
    {   
        string args="";
    //foreach(ColumnSchema col in this.CurentTable.Columns)
    //{
      //  if(col.IsPrimaryKeyMember)
        //{
        //args+=string.Format("{0}={1},",col.Name,col.Name);
        //} 
    //}
      
    foreach (MemberColumnSchema pkCol in this.CurentTable.PrimaryKey.MemberColumns)
    {
       args+=string.Format("{0},", pkCol.Column.Name);
    }
    return args.Substring(0,args.Length-1);
   }    public string PKArgsWithoutDataTypeForTable(TableSchema table)
    {   
        string args="";
    //foreach(ColumnSchema col in this.CurentTable.Columns)
    //{
      //  if(col.IsPrimaryKeyMember)
        //{
        //args+=string.Format("{0}={1},",col.Name,col.Name);
        //} 
    //}
      
    foreach (MemberColumnSchema pkCol in table.PrimaryKey.MemberColumns)
    {
       args+=string.Format("{0},", pkCol.Column.Name);
    }
    return args.Substring(0,args.Length-1);
   } public string GetFKArgs(TableKeySchema fk)
{
 string args="";
 foreach(MemberColumnSchema fkclo in fk.ForeignKeyMemberColumns)
{
    args+=string.Format("{0} {1},",fkclo.DataType,fkclo.Name);
    
    }
    return args.Substring(0,args.Length-1);
}
public string 主键在子表中的外键所包含的列的参数列表带数据类型(TableKeySchema fk)
{
    string args="";
    foreach(MemberColumnSchema fkclo in fk.ForeignKeyMemberColumns )
    {
         args+=string.Format("{0} {1},",fkclo.DataType,fkclo.Name);
    
    }
    return args.Substring(0,args.Length-1);
}
    
public string 主键在子表中的外键的参数列表(TableKeySchema fk)
{
    string args="";
    foreach(MemberColumnSchema fkclo in fk.ForeignKeyMemberColumns )
    {
         args+=string.Format("{0},",fkclo.Name);
    
    }
    return args.Substring(0,args.Length-1);
}
    
    </script>

解决方案 »

  1.   

    下面的是业务层模板代码:
    <%@ CodeTemplate  Inherits="CodeTemplate" Language="C#" TargetLanguage="Text"
     Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%>
    <%@ Assembly Name="SchemaExplorer" %>
    <%@ Assembly Name="System.Design" %>
    <%@ Assembly Name="System.DirectoryServices" %>
    <%@ Assembly Name="System.Web" %>
    <%@ Assembly Name="System.Xml" %><%@ Import Namespace="SchemaExplorer" %>
    <%@ Import NameSpace="System.IO" %>
    <%@ Import NameSpace="System.Text" %>
    <%@ Import NameSpace="System.Text.RegularExpressions" %>
    <%@ Import NameSpace="System.Diagnostics" %>
    <%@ Import NameSpace="System.Xml" %>
    <%@ Import NameSpace="System.Xml.Xsl" %>
    <%@ Import NameSpace="System.Xml.XPath" %> <%-- 1. Datasource --%>
    <%@ Property Name="CurrentTable" Type="SchemaExplorer.TableSchema" DeepLoad="True" Optional="False"  %>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace OA.BLL
    {
        public class <% = CurrentTable.Name%>
        {
            private DALFactory.DALFactory dataFactory = DataProvider.DefaultProvider;        #region 增
            public int 添加(Entity.<% = CurrentTable.Name%> obj)
            {
                //通过工厂获得当前提供程序
                //用反射,通过web.config获得当前提供程序的名,再实例化
                return dataFactory.DAL<% = CurrentTable.Name%>Provider.Insert(obj);
            }
           // public int 添加(string 机构名称, string 简称, int 排序)
          //  {
           //     return this.添加(Guid.NewGuid(), 机构名称, 简称, 排序);
          //  }
        
        //标准做法
        //方法:根据表,生成所有列构成的参数列表
            public int 添加(<%=this.AllArgumentsForTable()%>)
            {            return this.添加(new OA.Entity.<% = CurrentTable.Name%>()
                {
                   <% = this.AllPropertySetValueForTable() %>            });//生成实例类属性赋值    根据所有列,生成    列名= 参数名,
            }        #endregion        #region 改
            public int 修改(Entity.<% = CurrentTable.Name%> obj)
            {
               
                return dataFactory.DAL<% = CurrentTable.Name%>Provider.Update(obj);        }
            //方法:根据表,生成所有列构成的参数列表
            public int 修改(<%=this.AllArgumentsForTable()%>)
            {
                return this.修改(new OA.Entity.<% = CurrentTable.Name%>()
                {
                    <% = this.AllPropertySetValueForTable() %>
                });//生成实例类属性赋值         }
            #endregion        #region 删
            public int 删除(Entity.<% = CurrentTable.Name%> obj)
            {
                           return dataFactory.DAL<% = CurrentTable.Name%>Provider.Delete(obj);
            }
            
            //方法:生成列的主键列构成的参数列表
            public int 删除(<%= this.PKArgsForTable()%>)
            {
                return this.删除(new OA.Entity.<% = CurrentTable.Name%>()
                {
                    <%=this.PKPropertySetValueForTable()%>
                });
                //方法:生成主键列的赋值        }
            #endregion        #region 查
            //方法:生成列的主键列构成的参数列表
            public Entity.<% = CurrentTable.Name%> 取<% = CurrentTable.Name%>ById(<%= this.PKArgsForTable()%>)
            {
       
                return dataFactory.DAL<% = CurrentTable.Name%>Provider.LoadById(<%=this.PKArgsWithoutDataTypeForTable()%>);
            }
            public List<Entity.<% = CurrentTable.Name%>> 取所有<% = CurrentTable.Name%>()
            {
                return dataFactory.DAL<% = CurrentTable.Name%>Provider.LoadAll();        }
            //LoadXXXXChildEntitiesByParrentID()
           // public List<Entity.部门> 取属于机构的部门(Guid 机构ID)
          //  {
           //     return dataFactory.DAL机构Provider.Load部门By机构(机构ID);
           // }
         #endregion
        
        
           #region 当前表如果有外键,找外键所在的主表        <% foreach(TableKeySchema fk in this.CurrentTable.ForeignKeys){
                    
                        %>
                
            public Entity.<% = fk.PrimaryKeyTable.Name%> FindParrentBy<% = fk.Name%>FK(<%=this.GetFKArgs(fk) %>)
            {
                return this.dataFactory.DAL<% = fk.PrimaryKeyTable.Name%>Provider.LoadById(<% = PKArgsWithoutDataTypeForTable(fk.PrimaryKeyTable)%>);
            }
                
                <%}
                %>
                 #endregion
             
            #region 主表找从表   <%   foreach(TableKeySchema pk in this.CurrentTable.PrimaryKeys){
                    
                        %>
                        
            //从当前表,找子表的List
                 public List<Entity.<%= pk.ForeignKeyTable.Name%>> Get<%= pk.ForeignKeyTable.Name%>ListBy<%= pk.Name%>(<%= 主键在子表中的外键所包含的列的参数列表带数据类型(pk) %>)
            {
                
                   return this.dataFactory.DAL<%= pk.ForeignKeyTable.Name%>Provider.QurryList<%= pk.Name%>(<%=this.主键在子表中的外键所包含的列的参数列表(pk)%>); 
             
            }
            
            <%
            }
            %>
            #endregion
            
            
           
      

  2.   

     //////////////////测试使用
            
       <% foreach(TableKeySchema fk in this.CurrentTable.ForeignKeys){
                    
                   // Response.WriteLine("FK:{0}",fk.Name);
                    //fk:当前这个表,引用别的表的主键
                        }
                        
            foreach(TableKeySchema pk in this.CurrentTable.PrimaryKeys){
        
                Debug.WriteLine(pk.GetType().ToString());
      //  Response.WriteLine("PrimaryKey:{0}",pk.Name);
        //当前这个表的主键,在其他表中出现的外键引用
            }
                        
       // Response.WriteLine(this.CurrentTable.PrimaryKey.Name);//当前表的唯一主键
                        %>
                        
                        
            
        }
           
    }
    <script runat="template">    //方法:根据表,生成所有列构成的参数列表
        public string AllArgumentsForTable()
        {
            string args="";
            foreach(ColumnSchema col in this.CurrentTable.Columns)
            {
                args += string.Format("{0} {1},",col.DataType,col.Name);           
                
            }
            
            
            return args.Substring(0,args.Length-1);
            
            
        }
        //生成实例类属性赋值    根据所有列,生成    列名= 参数名,
          //方法:根据表,生成所有列构成的参数列表
        public string AllPropertySetValueForTable()
        {
            string args="";
            foreach(ColumnSchema col in this.CurrentTable.Columns)
            {
                args += string.Format("{0} = {1},",col.Name,col.Name);           
                
            }
            
            
            return args.Substring(0,args.Length-1);
            
            
        }
        //方法:生成列的主键列构成的参数列表
        public string PKArgsForTable()
        {
            string args="";
            //foreach(ColumnSchema col in this.CurrentTable.Columns)
           // {
          //      if(col.IsPrimaryKeyMember)
          //      {
          //      args += string.Format("{0} = {1},",col.Name,col.Name);           
          //      }
         //   }
            foreach(MemberColumnSchema pkCol in this.CurrentTable.PrimaryKey.MemberColumns)
            {
                    args += string.Format("{0} {1},",pkCol.Column.DataType,pkCol.Column.Name);  
            }
                       
             return args.Substring(0,args.Length-1);
        }
        //方法:生成主键列的赋值
            public string PKPropertySetValueForTable()
        {
            string args=""; 
            foreach(MemberColumnSchema pkCol in this.CurrentTable.PrimaryKey.MemberColumns)
            {
                    args += string.Format("{0} = {1},",pkCol.Column.Name,pkCol.Column.Name);  
            }
                       
             return args.Substring(0,args.Length-1);
        }
        
        //方法:生成主键列参数
        public string PKArgsWithoutDataTypeForTable()
        {
            string args=""; 
            foreach(MemberColumnSchema pkCol in this.CurrentTable.PrimaryKey.MemberColumns)
            {
                
                    args += string.Format("{0},",pkCol.Column.Name);
            }
                       
             return args.Substring(0,args.Length-1);
        }
        
         public string PKArgsWithoutDataTypeForTable(TableSchema table)
        {
            string args=""; 
            foreach(MemberColumnSchema pkCol in table.PrimaryKey.MemberColumns)
            {
                    args += string.Format("{0},",pkCol.Column.Name);
            }
                       
             return args.Substring(0,args.Length-1);
        }
        
        //按某个FK,取它的列构成的参数列表
        public string GetFKArgs(TableKeySchema fk)
        {
            
              string args=""; 
            foreach(MemberColumnSchema fkCol in fk.ForeignKeyMemberColumns)
            {
                    args += string.Format("{0} {1},",fkCol.DataType,fkCol.Name);
            }
                       
             return args.Substring(0,args.Length-1);
            }
        
        
        public string 主键在子表中的外键所包含的列的参数列表(TableKeySchema key){
          
             string args=""; 
            foreach(MemberColumnSchema Col in key.ForeignKeyMemberColumns)
            {
                    args += string.Format("{0},",Col.Column.Name);
            }
                       
             return args.Substring(0,args.Length-1);
            }
            
        public string 主键在子表中的外键所包含的列的参数列表带数据类型(TableKeySchema key){
          
             string args=""; 
            foreach(MemberColumnSchema Col in key.ForeignKeyMemberColumns)
            {
                    args += string.Format("{1} {0},",Col.Column.Name,Col.DataType);
            }
                       
             return args.Substring(0,args.Length-1);
            }
    </script>                        
            
      报错:#region 主表找从表 代码生成器生成的代码:
            public List<Entity.DirectoryInfo> GetDirectoryInfoListByFK_DIRECTOR_REFERENCE_FILESYST(Guid DirectoryInfoID)        {
                
                   return this.dataFactory.DALDirectoryInfoProvider.QuerryListFK_DirectoryInfo_REFERENCE_FileSystemInfo(DirectoryInfoID); 
             
            }
             
            public List<Entity.FileInfo> GetFileInfoListByFK_FILEINFO_REFERENCE_FILESYST(Guid FileInfoID)        {
                
                   return this.dataFactory.DALFileInfoProvider.QuerryListFK_FileInfo_REFERENCE_FileSystemInfo(FileInfoID); 
             
            }
             
    #endregion
     下面是被调用的接口层相应代码
    ////////////////////////////主表找子表
           List<Entity.DirectoryInfo> QuerryListFK_DIRECTOR_REFERENCE_DIRECTOR(Guid ParentDirectoryInfoID);
           List<Entity.DirectoryInfo> QuerryListFK_DIRECTOR_REFERENCE_FILESYST(Guid DirectoryInfoID);下面是vs 报错的显示:
      

  3.   

    本人的看法是代码没有错,因为我的数据库字段绝大多数表是中文字段,业务层和接口层生成的代码能够无缝对接,但是只有文件那块的表我我用的FileInfo 和FileSystemInfo继承DirectoryInfo.这里才用英文字段。