当前位置:编程学习 > C#/ASP.NET >>

实体类中的属性.怎么给他设置标记。

给属性设置一个标记。

[Key]               
public int UserInfoId { set; get; }

public string UserInfoName { set; get; }

比如说UserInfoID这个属性给他做一个标记。表示他是主键,可以在其他地方调用的时候能知道。
  这个是主键。 --------------------编程问答-------------------- 有谁知道怎么取这个[KEY]吗 --------------------编程问答-------------------- 感觉你给了[key]也没有用,因为数据主键是在数据库里,因为Model与数据库的表是ORM映射关系,结构一般都设计为一样,你要加的属性应该是类的系列化属性,即[Serializable]

[Serializable]
public class UserORM 
{
public int UserInfoId { set; get; }

public string UserInfoName { set; get; }

}
--------------------编程问答-------------------- 我就是想知道这个字段在数据库中是主键 --------------------编程问答-------------------- 你说的是注释吗?只要在前一行(空白)输入///就会自动打出来。
        /// <summary>
        /// 主键
        /// </summary>
        private int UserInfoID;
--------------------编程问答-------------------- 我是需要用反射拼接出SQL语句。
  SQL  Server还好说。 
   但是oracle就是比较麻烦。  --------------------编程问答-------------------- 在弄Insert语句的时候。 我需要知道哪个字段是主键。 
   有没有什么办法能在实体类把那个主键字段给标记出来。 --------------------编程问答-------------------- get,set都有了,直接调用就行了。 --------------------编程问答-------------------- Oracle  自动增长在SQL语句中需要写一个nextval才行啊 --------------------编程问答--------------------
引用 8 楼 youbeifang 的回复:
Oracle  自动增长在SQL语句中需要写一个nextval才行啊
 那你的问题也不在model加主键标记啊;  http://516263736.blog.163.com/blog/static/714114352010581513473/ --------------------编程问答--------------------
    class Model
    {        
        [Property("Test")]
        public string Name { get; set; }
    }

    class Property : System.Attribute
    {
        public string Value { get; set; }

        public Property(string Value)
        {
            this.Value = Value;
        }
    }
--------------------编程问答-------------------- 那在反射中怎么获取
--------------------编程问答--------------------
        static void Main(string[] args)
        {
            System.Reflection.PropertyInfo[] properties = typeof(Model).GetProperties();
            foreach (System.Reflection.PropertyInfo property in properties)
            {
                KeyFlagAttribute flag = property.GetCustomAttributes(typeof(KeyFlagAttribute), false)[0] as KeyFlagAttribute;
                if (flag.IsKey)
                {
                    Console.WriteLine(property.Name);
                    break;
                }
            }
            Console.ReadLine();
        }
    }

    public class Model
    {
        [KeyFlag(true)]
        public string UserInfoID { get; set; }
    }

    [AttributeUsage(AttributeTargets.Property)]
    public class KeyFlagAttribute : Attribute
    {
        private bool _bIsKey;
        public bool IsKey { get { return _bIsKey; } }
        public KeyFlagAttribute(bool bIsKey)
        {
            _bIsKey = bIsKey;
        }
    }
--------------------编程问答--------------------
引用 11 楼 youbeifang 的回复:
那在反射中怎么获取

    class Program
    {
        static void Main(string[] args)
        {
            PropertyInfo[] Properties = typeof(Model).GetProperties();

            foreach (PropertyInfo item in Properties)
            {
                object[] objArray = item.GetCustomAttributes(false);
                if (objArray.Length > 0)
                {
                    if ((item.GetCustomAttributes(false)[0] as Property).Value == "Key")
                        Console.WriteLine("属性:{0} 是Key主键", item.Name);
                }
            }
            Console.ReadKey();
        }
    }

    class Model
    {
        [Property("Key")]
        public string Name { get; set; }

        public string Age { get; set; }
    }

    class Property : System.Attribute
    {
        public string Value { get; set; }

        public Property(string Value)
        {
            this.Value = Value;
        }
    }
--------------------编程问答--------------------
    class Program
    {
        static void Main(string[] args)
        {
            PropertyInfo[] Properties = typeof(Model).GetProperties();

            foreach (PropertyInfo item in Properties)
            {
                object[] objArray = item.GetCustomAttributes(false);
                if (objArray.Length > 0)
                {
                    if ((objArray[0] as Property).Value == "Key")
                        Console.WriteLine("属性:{0} 是Key主键", item.Name);
                }
            }
            Console.ReadKey();
        }
    }

    class Model
    {
        public string Name { get; set; }

        [Property("Key")]
        public string Age { get; set; }
    }

    class Property : System.Attribute
    {
        public string Value { get; set; }

        public Property(string Value)
        {
            this.Value = Value;
        }
    }


这样给不同的属性做一个标记就可以知道那个是Key主键了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,