本文整理汇总了C#中MetadataWorkspace.TryGetType方法的典型用法代码示例。如果您正苦于以下问题:C# MetadataWorkspace.TryGetType方法的具体用法?C# MetadataWorkspace.TryGetType怎么用?C# MetadataWorkspace.TryGetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MetadataWorkspace
的用法示例。
在下文中一共展示了MetadataWorkspace.TryGetType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetEdmType
/// <summary>
/// Retrieves the <see cref="StructuralType"/> corresponding to the given CLR type (where the
/// type is an entity or complex type).
/// </summary>
/// <remarks>
/// If no mapping exists for <paramref name="clrType"/>, but one does exist for one of its base
/// types, we will return the mapping for the base type.
/// </remarks>
/// <param name="workspace">The <see cref="MetadataWorkspace"/></param>
/// <param name="clrType">The CLR type</param>
/// <returns>The <see cref="StructuralType"/> corresponding to that CLR type, or <c>null</c> if the Type
/// is not mapped.</returns>
public static StructuralType GetEdmType(MetadataWorkspace workspace, Type clrType)
{
if (workspace == null)
{
throw new ArgumentNullException("workspace");
}
if (clrType == null)
{
throw new ArgumentNullException("clrType");
}
if (clrType.IsPrimitive || clrType == typeof(object))
{
// want to avoid loading searching system assemblies for
// types we know aren't entity or complex types
return null;
}
// We first locate the EdmType in "OSpace", which matches the name and namespace of the CLR type
EdmType edmType = null;
do
{
if (!workspace.TryGetType(clrType.Name, clrType.Namespace, DataSpace.OSpace, out edmType))
{
// If EF could not find this type, it could be because it is not loaded into
// its current workspace. In this case, we explicitly load the assembly containing
// the CLR type and try again.
workspace.LoadFromAssembly(clrType.Assembly);
workspace.TryGetType(clrType.Name, clrType.Namespace, DataSpace.OSpace, out edmType);
}
}
while (edmType == null && (clrType = clrType.BaseType) != typeof(object) && clrType != null);
// Next we locate the StructuralType from the EdmType.
// This 2-step process is necessary when the types CLR namespace does not match Edm namespace.
// Look at the EdmEntityTypeAttribute on the generated entity classes to see this Edm namespace.
StructuralType structuralType = null;
if (edmType != null &&
(edmType.BuiltInTypeKind == BuiltInTypeKind.EntityType || edmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType))
{
workspace.TryGetEdmSpaceType((StructuralType)edmType, out structuralType);
}
return structuralType;
}
示例2: GetKeyPropertyNames
private static IEnumerable<string> GetKeyPropertyNames(Type type, MetadataWorkspace workspace)
{
EdmType edmType;
if (workspace.TryGetType(type.Name, type.Namespace, DataSpace.OSpace, out edmType))
{
return edmType.MetadataProperties.Where(mp => mp.Name == "KeyMembers")
.SelectMany(mp => mp.Value as ReadOnlyMetadataCollection<EdmMember>)
.OfType<EdmProperty>().Select(edmProperty => edmProperty.Name);
}
return null;
}
示例3: GetDeclaredPropertyNames
private static IEnumerable<string> GetDeclaredPropertyNames(Type type, MetadataWorkspace workspace)
{
EdmType edmType;
return workspace.TryGetType(type.Name, type.Namespace, DataSpace.OSpace, out edmType) ? ((System.Data.Entity.Core.Metadata.Edm.EntityType) edmType).DeclaredProperties.Select(x => x.Name) : null;
}