ArM*_*MaN 3 c# entity-framework ef-code-first entity-framework-5 entity-framework-6
我使用 EF 开发框架。我想获取实体的所有被忽略的属性来构建一些特殊查询。我该怎么做?
public class Customer
{
public int Id { get; set; }
public DateTime BirthDate { get; set; }
public int Age { get; set; }
}
public class CustomerContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Ignore(customer => customer.Age);
base.OnModelCreating(modelBuilder);
}
public DbSet<Customer> Customers { get; set; }
}
public static class DbContextExtensions
{
public static List<string> GetIgnoredProperties(this DbContext context, string entityTypeName)
{
// ???
}
}
Run Code Online (Sandbox Code Playgroud)
我知道这并没有回答你原来的问题,在我的评论中我提到你应该使用反射,但这只是因为我读错了你的问题。
如果你不正确,这里有一个使用反射的替代方法。
如果您将该[NotMapped]属性分配给您想要忽略的类上的属性,则可以[NotMapped]使用反射检索所有属性。以下是如何实现这一目标的示例。
var resultArray = yourClassInstance.GetType().GetProperties()
.Where(prop => Attribute.IsDefined(prop, typeof(NotMappedAttribute)));
Run Code Online (Sandbox Code Playgroud)
希望这能以某种方式帮助您。
| 归档时间: |
|
| 查看次数: |
2345 次 |
| 最近记录: |