簡體   English   中英

如何獲得主鍵

[英]How to get primary keys

我有一個模特

     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模型。 我想獲取這些模型的主鍵。

我有適用於應用程序的代碼

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

我沒有主鍵。 我想使用應用程序模型來獲取其他模型的密鑰。

您只需要調用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);
            }
        }

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

電話是

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

不確定您要做什么。 您的第一個代碼:

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

該屬性是PropertyInfo,因此它為您提供了主鍵的名稱。 a => a.GetType()== typeof(KeyAttribute)返回類的唯一標識符。 因此它正在返回ApplicationId。

您的第二個代碼不起作用,因為您要返回作為對象類型的CustomAttribute的名稱。

我認為您的第一個代碼應適用於所有3個類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM