是否可以将属性设置为EF中的特定表/类,然后根据该属性查询这些表的列表?获取特定表的列表?
0
A
回答
1
添加属性partial
类。然后得到一个类型列表与当前装配
Assembly thisAsm = Assembly.GetExecutingAssembly();
var tableTypes = thisAsm.GetTypes()
.Where(t => t.IsDefined(typeof(MyAttribute), false));
0
好这个属性,我觉得我得到它的工作,而无需使用大会:
public class FoodAttribute : Attribute { }
public class Fruit { }
public class Coin { }
public class Cereal { }
public class FooContext : DbContext {
[Food]
public virtual DbSet<Fruit> Fruits { get; set; }
public virtual DbSet<Coin> Coins { get; set; }
[Food]
public virtual DbSet<Cereal> Cereals { get; set; }
}
要获得的属性食物表的列表:
var tableList = typeof(FooContext).GetProperties()
.Where(n => n.IsDefined(typeof(ClientTraitAttribute)))
.Select(n => n.PropertyType.GetGenericArguments()[0].Name).ToList();
感谢Yuriy向我展示IsDefined方法! :D
+0
DbContext文件不是自动生成的吗?除非你手动做。 –
+0
对,我从数据库(即数据库优先)开始执行Code First,因此这些文件只生成一次。 – Alex
相关问题
- 1. 从特定列获取值列表
- 2. 获取特定TargetType的样式列表
- 3. 获取特定类型的豆列表
- 4. 取列名的特定表
- 5. 使用msforeachtable获取特定数据库的表的列表
- 6. 从特定播放列表中获取特定歌曲
- 7. 使用javascript从MVC列表中获取特定的列
- 8. 如何获取特定列的记录列表家庭?
- 9. Sharepoint如何获取Sharepoint特定字段/列的列表
- 10. 从选定的列表中获取特定对象
- 11. 定义对象列表。获取对象中的特定字段
- 12. 如何获取表格的特定行?
- 13. 获取Excel工作表的特定行
- 14. BeautifulSoup:获取特定表格的内容
- 15. 没有获得特定表的列
- 16. 读取Excel工作表的特定列
- 17. 获取特定列表元素从一系列
- 18. 如何获取数据表的特定列的唯一记录
- 19. 获取完整的特定指数的股票列表
- 20. 获取的两个列表与特定标准的交叉
- 21. 获取跨多个表的特定列的所有不同值
- 22. 获取特定于其中元素的子列表的索引
- 23. 获取与所有者的特定上下文的列表
- 24. 迭代特定列的表列表
- 25. Tweepy获取特定用户的收藏列表
- 26. 如何获取特定Python模块中的变量列表?
- 27. 从列表中获取特定类型的对象<T>
- 28. 获取特定库文件的函数列表
- 29. 获取尚未集成到特定分支的更改列表
- 30. 如何获取特定知识库的列表?
谢谢你向我展示IsDefined方法,我直接使用DbContext类来处理它。 – Alex