gpt4 book ai didi

c# - 从 EntityFramework ObjectContext 获取类型集合

转载 作者:行者123 更新时间:2023-11-30 15:12:31 25 4
gpt4 key购买 nike

如何从 ObjectContext 中提取 Types 列表?

例如,我的对象上下文包含名为“Bank”的实体和名为“Company”的实体。我想获取它们的 EntityObject 类型。

我该怎么做?

最佳答案

我假设您在运行时想要查询生成的 ObjectContext获取 EntityObject 列表的类类。然后它变成了反射(reflection)练习:

PropertyInfo[] propertyInfos = objectContext.GetType().GetProperties();
IEnumerable<Type> entityObjectTypes =
from propertyInfo in propertyInfos
let propertyType = propertyInfo.PropertyType
where propertyType.IsGenericType
&& propertyType.Namespace == "System.Data.Objects"
&& propertyType.Name == "ObjectQuery`1"
&& propertyType.GetGenericArguments()[0].IsSubclassOf(typeof(EntityObject))
select propertyType.GetGenericArguments()[0];

此代码将在类型为 System.Data.Objects.ObjectQuery<T> 的对象上下文中找到所有公共(public)属性其中 TEntityObject 的子类.

关于c# - 从 EntityFramework ObjectContext 获取类型集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1184208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com