自定义函数

可空类型处理

<%--遍历数据库表的字段属性--%>
<% foreach (ColumnSchema column in this.SourceTable.Columns) {  %>
<%--拼接字符串,输出c#中实体的属性--%>
public <%= ControlType(CSharpAlias[column.SystemType.FullName]) %> <%= column.Name %>{ get; set; }

<% } %>

<script runat="template">
 //如果类型为int,或datetime类型输出可空类型
 public string ControlType(object val)
 {
     var ty=val.ToString();
     if(ty=="int")
     {
         return "int?";
     }
     if(ty=="System.DateTime")
     {
         return "System.DateTime?";
     }
     return ty;
 }
</script>Code language: HTML, XML (xml)

生成结果:

public int? AccountID{ get; set; }

public int? UniqueID{ get; set; }

public string Email{ get; set; }

public string FirstName{ get; set; }

public string LastName{ get; set; }

public string Address1{ get; set; }

public string Address2{ get; set; }

public string City{ get; set; }

public string State{ get; set; }

public string Zip{ get; set; }

public string Country{ get; set; }

public string Phone{ get; set; }Code language: JavaScript (javascript)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注