Skip to content

Commit 30d6a74

Browse files
authored
add GetEntityTypeByType for ethod that gets entity type by type
add GetEntityTypeByType for ethod that gets entity type by type
1 parent 312c728 commit 30d6a74

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

GraphDiff/GraphDiff/Internal/Extensions.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Data.Entity.Core.Metadata.Edm;
44
using System.Data.Entity.Core.Objects;
@@ -11,10 +11,9 @@ namespace RefactorThis.GraphDiff.Internal
1111
internal static class Extensions
1212
{
1313
internal static IEnumerable<PropertyInfo> GetPrimaryKeyFieldsFor(this IObjectContextAdapter context, Type entityType)
14-
{
14+
{
1515
var metadata = context.ObjectContext.MetadataWorkspace
16-
.GetItems<EntityType>(DataSpace.OSpace)
17-
.SingleOrDefault(p => p.FullName == entityType.FullName);
16+
.GetEntityTypeByType(entityType);
1817

1918
if (metadata == null)
2019
{
@@ -32,10 +31,8 @@ internal static IEnumerable<NavigationProperty> GetRequiredNavigationPropertiesF
3231

3332
internal static IEnumerable<NavigationProperty> GetNavigationPropertiesForType(this IObjectContextAdapter context, Type entityType)
3433
{
35-
return context.ObjectContext.MetadataWorkspace
36-
.GetItems<EntityType>(DataSpace.OSpace)
37-
.Single(p => p.FullName == entityType.FullName)
38-
.NavigationProperties;
34+
35+
return context.ObjectContext.MetadataWorkspace.GetEntityTypeByType(entityType).NavigationProperties;
3936
}
4037

4138
internal static string GetEntitySetName(this IObjectContextAdapter context, Type entityType)
@@ -55,5 +52,25 @@ internal static string GetEntitySetName(this IObjectContextAdapter context, Type
5552

5653
return set != null ? set.Name : null;
5754
}
55+
56+
/// <summary>A MetadataWorkspace extension method that gets entity type by type.</summary>
57+
/// <param name="metadataWorkspace">The metadataWorkspace to act on.</param>
58+
/// <param name="entityType">Type of the entity.</param>
59+
/// <returns>The entity type by type.</returns>
60+
/// not support class in generic class
61+
internal static EntityType GetEntityTypeByType(this MetadataWorkspace metadataWorkspace, Type entityType)
62+
{
63+
string name = entityType.FullName.Replace("+", ".");
64+
var lenght = name.IndexOf("`");
65+
66+
if (lenght != -1)
67+
{
68+
name = name.Substring(0, lenght);
69+
}
70+
71+
return metadataWorkspace
72+
.GetItems<EntityType>(DataSpace.OSpace)
73+
.SingleOrDefault(p => p.FullName == name);
74+
}
5875
}
5976
}

0 commit comments

Comments
 (0)