如何获得主键

时间:2017-11-03 15:02:19

标签: c# reflection

我有一个模特

     public class Application : BaseModel, IApplication
     {
       [Key]
       public int ApplicationId { get; set; }

       public DateTime? Date { get; set; }

       public string ApplicationTypeId { get; set; }

       public List<AlternateSigningAuthority> AlternateSigningAuthorities { get; set; }

       public List<Land> Lands { get; set; }

     }

另一个模特

     public class Land : BaseModel, IEquatable<Land>/*, ILand*/
    {

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

       [ForeignKey("ApplicationId")]
        public int ApplicationId { get; set; }

        public int Unit { get; set; }
    }

类似我有AlternateSigningAuthority的模型。我想获得这些模型的主键。

我有适用于Application

的代码
           var type = typeof(Application);
           var properties = type.GetProperties();

           var property = properties
                .FirstOrDefault(p => p.GetCustomAttributes(false)
                    .Any(a => a.GetType() == typeof(KeyAttribute)));

            if (property != null)
            {
                string msg = "The Primary Key for the {0} class is the {1} property";
                Console.WriteLine(msg, type.Name, property.Name);
            }

我加上

    foreach (var prop in properties)
        {
            var attributes = property.GetCustomAttributes(false);
            foreach (var attribute in attributes)
            {
                if (attribute.GetType() == typeof(KeyAttribute))
                {
                    string msg = "The Primary Key for the {0} class is the {1} property";
                    Console.WriteLine(msg, type.Name, attribute.ToString());
                }
            }

我没有获得主键。我想使用Application Model来获取其他模型的密钥。

3 个答案:

答案 0 :(得分:3)

您只需要调用Attribute.GetCustomAttribute

AddressViewModel viewModelInstance=new AddressViewModel();
        PropertyInfo[] properties = viewModelInstance.GetType().GetProperties();

        foreach (PropertyInfo property in properties)
        {
            var attribute = Attribute.GetCustomAttribute(property, typeof(KeyAttribute))
                as KeyAttribute;

            if (attribute != null) // This property has a KeyAttribute
            {
                // Do something, to read from the property:
                object val = property.GetValue(viewModelInstance);
            }
        }

我引用了此链接Get [key] property from ViewModel

答案 1 :(得分:1)

public void WriteKeyProperty(Type classType)
{
    var properties = classType.GetProperties();
    foreach (var property in properties)
    {
        var keyAttribute = property.GetCustomAttribute(typeof(KeyAttribute));
        if (keyAttribute == null) continue;
        const string msg = "The Primary Key for the {0} class is the {1} property";
        Console.WriteLine(msg, classType.Name, property.Name);
    }
}

电话是

WriteKeyProperty(typeof(Application));
WriteKeyProperty(typeof(Land));

答案 2 :(得分:0)

不确定你想要做什么。你的第一个代码:

var property = properties
                .FirstOrDefault(p => p.GetCustomAttributes(false)
                    .Any(a => a.GetType() == typeof(KeyAttribute)));

属性是PropertyInfo,因此它为您提供主键的名称。 a =&gt; a.GetType()== typeof(KeyAttribute)返回类的唯一标识符。所以它返回了ApplicationId。

您的第二个代码无法正常工作,因为您正在返回作为对象类型的CustomAttribute的名称。

我认为您的第一个代码应适用于所有3个类。