[英]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.