[英]How to get a list of the field names in a table in EF 5
我的實體定義如下,
[Table("AuditZone")]
public class AuditZone
{
public AuditZone()
{
AuditZoneUploadedCOESDetails = new List<UploadedCOESDetails>();
AuditZonePostcode = new List<Postcodes>();
}
[Key]
[DoNotAudit]
public int Id { get; set; }
public string Description { get; set; }
public bool Valid { get; set; }
[DoNotAudit]
public DateTime CreatedDate { get; set; }
[DoNotAudit]
public int? CreatedBy { get; set; }
[DoNotAudit]
public DateTime? ModifiedDate { get; set; }
[DoNotAudit]
public int? ModifiedBy { get; set; }
public virtual UserProfile CreatedByUser { get; set; }
public virtual UserProfile ModifiedByUser { get; set; }
public virtual ICollection<UploadedCOESDetails> AuditZoneUploadedCOESDetails { get; set; }
public virtual ICollection<Postcodes> AuditZonePostcode { get; set; }
}
}
一些字段具有名為DoNotAudit
的屬性。
我需要能夠獲得表中沒有該DoNotAudit
屬性的字段的列表。
我嘗試了以下操作,但不起作用。 有任何想法嗎 ?
public IEnumerable<string> GetFields(string tableName)
{
var table = typeof(AISDbContext).GetProperties().Select(n => n.PropertyType) // here we get all properties of contect class
.Where(n => n.Name.Contains("DbSet") && n.IsGenericType) // here we select only DBSet collections
.Select(n => n.GetGenericArguments()[0])
.Where(n => n.Name == tableName);
var doNotAuditList = table.GetType()
.GetProperties()
.Where(p => p.GetCustomAttributes(typeof(DoNotAudit), false).Any())
.Select(p => p.Name)
.ToList();
return doNotAuditList;
}
更新查詢
var doNotAuditList = table.First().GetProperties()
.Where(p=> p.PropertyType.FindInterfaces(new TypeFilter((t,o) => t == typeof(IEnumerable)), null).Length == 0)
.Where(n => n.GetCustomAttributes(true).OfType<DoNotAudit>().FirstOrDefault() == null)
.Select(p => p.Name)
.ToList();
table
已經是您需要的類型,因此table.GetType()
不正確。 只需使用table.GetProperties()
如下:
var doNotAuditList = table.First()
.GetProperties()
.Where(p => p.GetCustomAttributes(typeof(DoNotAudit), false).Any() == false
&& p.GetGetMethod().IsVirtual == false)
.Select(p => p.Name)
.ToList();
編輯: table
的類型為IQueriable<Type>
,因此需要對.First()
的調用才能獲取實際的對象。
編輯更新了Linq查詢,以忽略所有虛擬屬性和標記為DoNotAudit
屬性。
類型typeof(Poco).GetProperties()
可能導致與EF中實際跟蹤的結果不同。 EF將忽略某些類型,某些類型可能具有忽略注釋。 復雜類型需要特別注意。
如果要模型的EF視圖,請訪問MetadataWorkspace
。
一些基本的轉儲元數據來調試控制台代碼...
[TestMethod]
public void EFToolsTest() {
// http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.dataspace(v=vs.110).aspx
var context = new YourContext(); // DbContext type
ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext;
MetadataWorkspace workspace = objContext.MetadataWorkspace;
var xyz = workspace.GetItems<EntityType>(DataSpace.SSpace);
foreach (var ET in xyz) {
foreach (var sp in ET.Properties) {
Debug.WriteLine(sp.Name + ":" + sp.MaxLength);// just as an example
}
}
}
或通過DataSpace.OSpace
public static void DumpContextManagedTypeProps() {
var context = new YourContent();
ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext;
MetadataWorkspace workspace = objContext.MetadataWorkspace;
IEnumerable<EntityType> managedTypes = workspace.GetItems<EntityType>(DataSpace.OSpace);
foreach (var managedType in managedTypes.Where(mt=>mt.Ful) {
Console.WriteLine(managedType.FullName);
// propertyInfo and other useful info is available....
foreach ( var p in managedType.Properties) {
Console.WriteLine(p.Name );
}
}
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.