獲取此類屬性時如何跳過計算列?我可以為 NotMapped 做到這一點,但不確定DatabaseGenerated(DatabaseGeneratedOption.Computed)?
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public bool AAUProcessing { get; set; }
跳過 NotMapped 和 Computred 列
var props = typeof(TK).GetProperties()
.Where(propertyInfo => !propertyInfo.GetCustomAttributes(typeof(NotMappedAttribute)).Any())
.ToArray();
uj5u.com熱心網友回復:
只需將其轉換為正確的型別 ( DatabaseGeneratedAttribute),您就可以檢查它的任何您認為合適的屬性。
以下示例將過濾掉計算和未映射的屬性:
void Main()
{
var props = typeof(TK).GetProperties()
.Where(IsNotMapped)
.Where(IsNotComputedColumn)
.ToArray();
foreach (var property in props)
{
Console.WriteLine(property.Name);
}
}
static bool IsNotMapped(PropertyInfo propertyInfo)
{
return !propertyInfo
.GetCustomAttributes(typeof(NotMappedAttribute))
.Any();
}
static bool IsNotComputedColumn(PropertyInfo propertyInfo)
{
return !propertyInfo
.GetCustomAttributes(typeof(DatabaseGeneratedAttribute))
.Cast<DatabaseGeneratedAttribute>()
.Any(a => a.DatabaseGeneratedOption == DatabaseGeneratedOption.Computed);
}
public class TK
{
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public bool IsComputed { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public bool IsIdentity { get; set; }
[NotMapped]
public bool NotMapped { get; set; }
public bool StandardColumn { get; set; }
}
輸出是
IsIdentity
StandardColumn
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/313308.html
