[英]How to get a list of the field names in a table in EF 5
I have my entities defined as follows, 我的实体定义如下,
[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; }
}
}
Some of the fields have an attribute called DoNotAudit
. 一些字段具有名为
DoNotAudit
的属性。
I need to be able to get a list of the fields in the table that do not have that DoNotAudit
attribute. 我需要能够获得表中没有该
DoNotAudit
属性的字段的列表。
I tried the following, but it doesn't work. 我尝试了以下操作,但不起作用。 Any ideas ?
有任何想法吗 ?
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;
}
Updated query 更新查询
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
is already the type you need, so table.GetType()
is not correct. table
已经是您需要的类型,因此table.GetType()
不正确。 Just use table.GetProperties()
as below: 只需使用
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();
EDIT: table
is of type IQueriable<Type>
, so a call to .First()
is needed to get the actual object. 编辑:
table
的类型为IQueriable<Type>
,因此需要对.First()
的调用才能获取实际的对象。
EDIT Updated Linq query to ignore all virtual properties and properties marked DoNotAudit
. 编辑更新了Linq查询,以忽略所有虚拟属性和标记为
DoNotAudit
属性。
Reflection of the Type typeof(Poco).GetProperties()
can lead to different results to what is actually tracked in EF. 类型
typeof(Poco).GetProperties()
可能导致与EF中实际跟踪的结果不同。 EF will ignore some types, some types may have Ignore annotations. EF将忽略某些类型,某些类型可能具有忽略注释。 Complex Types need special attention.
复杂类型需要特别注意。
If you want the EF view of the model, then access the MetadataWorkspace
. 如果要模型的EF视图,请访问
MetadataWorkspace
。
Some basic dump Metadata to debug console code... 一些基本的转储元数据来调试控制台代码...
[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
}
}
}
or via the DataSpace.OSpace 或通过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.