遍历派生的DbContext DbSet,以便可以向我的DbContext对象添加ClearAll()扩展 [英] Looping over derived DbContext DbSets so I can add a ClearAll() extension to my DbContext object
问题描述
基于[ Type.IsGenericType
与 Type.IsGenericTypeDefinition
之间的区别] [1],我获得了为了能够通过反射检索DbContext的DbSet,我缺少了一条信息。
Based on [the difference between Type.IsGenericType
and Type.IsGenericTypeDefinition
][1], I gained the piece of information I was missing in order to be able to retrieve a DbContext's DbSets via Reflection.
但是,我现在被困在其他地方。在下面的代码示例中,我确实成功获取了DbSet泛型实例,但是现在我希望能够在编译时将其强制转换为特定的泛型类型定义。
However, I am now stuck somewhere else. In the code sample below, I do succeed in obtaining the DbSet generic instances, but I now want to be able to cast them at compile time, to the specific generic type defintion.
dbSet是我的DbContext中DbSets< ...>之一的真实实例,但是如何将其转换为实型定义? (在第一个循环迭代中,dBSet将代表DbSet-如何在编译时对该变量执行此类型?
dbSet is a real instance of one of the DbSets<...> from my DbContext, but how to cast it to its real type definition ? (Take the 1st loop iteration, dBSet will represent DbSet - how to enforce this type at compile time upon this variable ?
更新:我刚刚回顾了我要尝试做的事情,...这恐怕是没有意义的/不可能的。由于实际类型只能在运行时发现,因此我无法在此之前(并且编译时间是在此之前的时间)可以与真正的泛型类型相关。
Update: I have just reviewed what I am trying to do and... am afraid it makes no sense/it is not possible. Since the real types are discovered only at runtime, there is no way I can, prior to that (and compile time is a stage in time prior to that) relate to the real generic types.
无论如何-我真正感兴趣的是是要在编译时向IDE显示 IEnumerable< T>
的成员(DbSet<>实现了它),所以我真的需要将其强制转换为 DbSet<>
。这样的转换应如何显示?
In any case - what I am really interested to is to have, at compile time, the IDE showing me members of IEnumerable<T>
(DbSet<> implements it). So I really need to cast it to DbSet<>
. How does such a cast should look like ?
有可能吗?!
private static void Main(string[] args)
{
MyDb myDb = new MyDb();
List<PropertyInfo> dbSetProperties = myDb
.GetType()
.GetProperties()
.Where(pi => pi.PropertyType.IsGenericType && string.Equals(pi.PropertyType.GetGenericTypeDefinition().FullName, typeof(DbSet<>).FullName, StringComparison.OrdinalIgnoreCase)).ToList();
foreach (PropertyInfo dbSetProperty in dbSetProperties)
{
Type[] typeArguments = dbSetProperty.PropertyType.GetGenericArguments();
Type dbSetGenericContainer = typeof(DbSet<>);
Type dbSetGenericType = dbSetGenericContainer.MakeGenericType(typeArguments);
object dbSet = Convert.ChangeType(dbSetProperty.GetValue(myDb), dbSetGenericType);
}
}
public class MyDb : DbContext
{
public DbSet<A> A { get; set; }
public DbSet<B> B { get; set; }
public void ClearTables()
{
}
}
public class A
{
public string aaa { get; set; }
public List<int> myList { get; set; }
}
public class B
{
public int MyInt { get; set; }
}
推荐答案
DbSet<>
本身不是类型;您的问题实际上没有任何意义。
DbSet<>
itself is not a type; your question doesn't actually make sense.
但是,幸运的是, DbSet&T;
实现了非-generic DbSet
基类,因此您可以改用它。
However, luckily for you, DbSet<T>
implements the non-generic DbSet
base class, so you can just use that instead.
这篇关于遍历派生的DbContext DbSet,以便可以向我的DbContext对象添加ClearAll()扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!