简体   繁体   English

如何获得主键

[英]How to get primary keys

I have a model 我有一个模特

     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; }

     }

And another model 还有另一种模式

     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; }
    }

Similary I have a model for AlternateSigningAuthority. 类似地,我有一个AlternateSigningAuthority模型。 I want to get Primary Keys of these models. 我想获取这些模型的主键。

I have code which works for 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);
            }

AS soon as I add 只要我添加

    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());
                }
            }

I dont get Primary keys. 我没有主键。 I want to use Application Model to get keys for other models. 我想使用应用程序模型来获取其他模型的密钥。

You just need to call Attribute.GetCustomAttribute 您只需要调用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);
            }
        }

I referred this link Get [key] property from ViewModel 从ViewModel引用了此链接Get [key]属性

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);
    }
}

And the call is 电话是

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

No sure what u are trying to do. 不确定您要做什么。 your first code: 您的第一个代码:

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

the property is PropertyInfo, therefore it gives you the name of the primary key. 该属性是PropertyInfo,因此它为您提供了主键的名称。 The a => a.GetType() == typeof(KeyAttribute) return the uniquely identifier of the class. a => a.GetType()== typeof(KeyAttribute)返回类的唯一标识符。 So it is returning ApplicationId. 因此它正在返回ApplicationId。

Your second code is not working because you are returning name of the CustomAttribute which is a object type. 您的第二个代码不起作用,因为您要返回作为对象类型的CustomAttribute的名称。

I think your first code should work for all 3 classes. 我认为您的第一个代码应适用于所有3个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM