Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架4:通用存储库:如何确定EntitySetName?_C#_Reflection_Entity Framework 4 - Fatal编程技术网

C# 实体框架4:通用存储库:如何确定EntitySetName?

C# 实体框架4:通用存储库:如何确定EntitySetName?,c#,reflection,entity-framework-4,C#,Reflection,Entity Framework 4,如果在Entity Framework 4中为某个实体创建通用存储库,则首先查询该实体: public IEnumerable<E> GetEntity() { return _container.CreateQuery<E>( ... ); } 如果我们有一个真实的实体,它将包含我们的EntitySetName: E.EntityKey.EntitySetName 如果只提供E的类型,如何获取EntitySetName?这很棘手,尤其是当涉及代理时,但这是可

如果在Entity Framework 4中为某个实体创建通用存储库,则首先查询该实体:

public IEnumerable<E> GetEntity()
{
    return _container.CreateQuery<E>( ... );
}
如果我们有一个真实的实体,它将包含我们的EntitySetName:

E.EntityKey.EntitySetName

如果只提供E的类型,如何获取EntitySetName?

这很棘手,尤其是当涉及代理时,但这是可能的。我是这样做的:

//
///返回给定实体类型的实体集名称
/// 
///定义entityType的实体集的ObjectContext。必须为非null。
///实体类型。必须为非null,并且在上下文参数中定义了实体集。
///如果entityType不是实体或没有在上下文中定义实体集。
///实体集的字符串名称。
内部静态字符串GetEntitySetName(此ObjectContext上下文,类型entityType)
{
if(上下文==null)
{
抛出新的ArgumentNullException(“上下文”);
}
if(entityType==null)
{
抛出新ArgumentNullException(“entityType”);
}
//启用POCO代理时,“entityType”可能是映射类型的子类型。
类型nonProxyEntityType=ObjectContext.GetObjectType(entityType);
if(entityType==null)
{
抛出新的ArgumentException(
string.Format(System.Globalization.CultureInfo.CurrentUICulture,
Halfpipe.Resource.typeisnotanity,
entityType.Name));
}
var container=context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName,System.Data.Metadata.Edm.DataSpace.CSpace);
var result=(来自container.BaseEntitySets中的entitySet
其中entitySet.ElementType.Name.Equals(非ProxyEntityType.Name)
选择entitySet.Name).SingleOrDefault();
if(string.IsNullOrEmpty(result))
{
抛出新的ArgumentException(
string.Format(System.Globalization.CultureInfo.CurrentUICulture,
Halfpipe.Resource.typeisnotanity,
entityType.Name));
}
返回结果;
}
E.EntityKey.EntitySetName
    /// <summary>
    /// Returns entity set name for a given entity type
    /// </summary>
    /// <param name="context">An ObjectContext which defines the entity set for entityType. Must be non-null.</param>
    /// <param name="entityType">An entity type. Must be non-null and have an entity set defined in the context argument.</param>
    /// <exception cref="ArgumentException">If entityType is not an entity or has no entity set defined in context.</exception>
    /// <returns>String name of the entity set.</returns>
    internal static string GetEntitySetName(this ObjectContext context, Type entityType)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        if (entityType == null)
        {
            throw new ArgumentNullException("entityType");
        }
        // when POCO proxies are enabled, "entityType" may be a subtype of the mapped type.
        Type nonProxyEntityType = ObjectContext.GetObjectType(entityType);
        if (entityType == null)
        {
            throw new ArgumentException(
                string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                Halfpipe.Resource.TypeIsNotAnEntity,
                entityType.Name));
        }

        var container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, System.Data.Metadata.Edm.DataSpace.CSpace);
        var result = (from entitySet in container.BaseEntitySets
                      where entitySet.ElementType.Name.Equals(nonProxyEntityType.Name)
                      select entitySet.Name).SingleOrDefault();
        if (string.IsNullOrEmpty(result))
        {
            throw new ArgumentException(
                string.Format(System.Globalization.CultureInfo.CurrentUICulture,
                Halfpipe.Resource.TypeIsNotAnEntity,
                entityType.Name));
        }
        return result;
    }