1. 程式人生 > >EF6.0實體類添加註釋

EF6.0實體類添加註釋

第一步: 在Model.tt檔案新增一個方法讀取資料庫中的欄位說明

public string GetRemark(EdmProperty edmProperty){
		//System.Diagnostics.Debugger.Launch();
		var tableName = edmProperty.DeclaringType.Name;
		var colName=_code.Escape(edmProperty);
		//System.Diagnostics.Debugger.Break();
		string sql = string.Format(@"SELECT
欄位說明=isnull(props.[value],'')
FROM syscolumns cols
inner join sysobjects objs on cols.id= objs.id and  objs.xtype='U' and  objs.name<>'dtproperties'
left join sys.extended_properties props on cols.id=props.major_id and cols.colid=props.minor_id
where  objs.name='{0}' and cols.name='{1}'",tableName,colName);
		string sqlcon = "Data Source=192.168.1.102;User ID=sa;Password=123456;Initial Catalog=DTTT";
		object remark=new object();
		using(System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(sqlcon)){
			conn.Open();
			System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, conn);
			remark =cmd.ExecuteScalar();    
		}      
		
	return remark+"";
	}
第二步:修改如下
<#
    }

    var simpleProperties = typeMapper.GetSimpleProperties(entity);
    if (simpleProperties.Any())
    {
        foreach (var edmProperty in simpleProperties)
        {
		//實體屬性開始
#>

	/// <summary>
	/// <#=codeStringGenerator.GetRemark(edmProperty)#>
	/// </summary>
    <#=codeStringGenerator.Property(edmProperty)#>

<#
		//實體屬性結束
        }
    }

    if (complexProperties.Any())
    {
#>
儲存自動支行T4檔案,註釋就出來了.