检测表的主键

  • 本文关键字: c# reflection t4
  • 更新时间 :
  • 英文 :


我正在尝试生成一些自定义实体和自定义方法来从数据库中获取数据。

ListByPrimaryKey-列名有时是"ID"、"pid"、"ID"等。

如何使用C#和T4模板检测列是主键?

protected void GenerateListByMethods( Table table)
{
    WriteLine("#region ListByn");
    WriteLine("ntpublic List<"+Prefix+table.Name+"> ListAll()nt{nt  " +
        this.RepositoryNotification +
        "nttt return Ctx.GetAll().ToList().ToCustomList<"+table.Name+","+Prefix+table.Name+">();nt}n");
    foreach (Column  column in table.Columns)
    {
        switch(column.DataType.Name)
        {  // do something

如何编写column.DataType.IsPrimaryKey的扩展方法?

如果您正在使用SQL SERVER,请尝试运行以下任一查询

SELECT  i.name AS IndexName,
        OBJECT_NAME(ic.OBJECT_ID) AS TableName,
        COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM    sys.indexes AS i INNER JOIN 
        sys.index_columns AS ic ON  i.OBJECT_ID = ic.OBJECT_ID
                                AND i.index_id = ic.index_id
WHERE   i.is_primary_key = 1

select * 
from sysobjects 
where xtype='pk' and 
   parent_obj in (select id from sysobjects where name='tablename')

相关内容

  • 没有找到相关文章

最新更新