如何获取entityframework中的entity的列名

来源:互联网 发布:免备案一级域名 编辑:程序博客网 时间:2024/05/16 02:35

try this by using LINQ-TO-OBJECT to find out all the properties, and then use GetCustomAttributes to find out which fits well.

通过GetProperties()方法获取列名list,即linq to sql 语句获取;

namespace nibian{    /// <summary>    /// Suppose this is a class that has two "Required" attributes    /// </summary>    public class Model    {        [Required]        public int Id { getset; }        [Required]        public string Name { getset; }        public double Score { getset; }    }     public class MainTest    {        static void Main(string[] args)        {            // This will search for each Attributes applied onto the property            // to find out whether "Required" exists or not.            var properties = typeof(Model).GetProperties().Where(p => p.GetCustomAttributes(typeof(RequiredAttribute), false).Length==1).Select(p=>p);             // This will output the Name of the assigned public properties.            foreach (var item in properties)            {                Console.WriteLine(item.Name);            }         }    }}

 

原创粉丝点击