-
Notifications
You must be signed in to change notification settings - Fork 32
/
OeEfCoreEdmModelMetadataProvider.cs
140 lines (124 loc) · 6.13 KB
/
OeEfCoreEdmModelMetadataProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using OdataToEntity.ModelBuilder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace OdataToEntity.EfCore
{
public sealed class OeEfCoreEdmModelMetadataProvider : OeEdmModelMetadataProvider
{
private readonly IModel _efModel;
private readonly Dictionary<Type, IEntityType> _entityTypes;
public OeEfCoreEdmModelMetadataProvider(IModel model)
{
_efModel = model;
_entityTypes = _efModel.GetEntityTypes().ToDictionary(e => e.ClrType);
}
private IEnumerable<IEntityType> GetEntityTypes(PropertyInfo propertyInfo)
{
if (_entityTypes.TryGetValue(propertyInfo.DeclaringType, out IEntityType efEntityType))
yield return efEntityType;
else
foreach (KeyValuePair<Type, IEntityType> pair in _entityTypes)
if (propertyInfo.DeclaringType.IsAssignableFrom(pair.Key))
yield return pair.Value;
}
public override PropertyInfo[] GetForeignKey(PropertyInfo propertyInfo)
{
foreach (IEntityType efEntityType in GetEntityTypes(propertyInfo))
foreach (IForeignKey fkey in efEntityType.GetForeignKeys())
{
if (fkey.PrincipalEntityType == efEntityType && fkey.PrincipalEntityType != fkey.DeclaringEntityType)
continue;
if (fkey.DependentToPrincipal.Name == propertyInfo.Name)
{
var propertyInfos = new PropertyInfo[fkey.Properties.Count];
for (int i = 0; i < fkey.Properties.Count; i++)
propertyInfos[i] = propertyInfo.DeclaringType.GetPropertyIgnoreCase(fkey.Properties[i].Name);
return propertyInfos;
}
for (int i = 0; i < fkey.Properties.Count; i++)
if (fkey.Properties[i].Name == propertyInfo.Name)
return new PropertyInfo[] { propertyInfo.DeclaringType.GetPropertyIgnoreCase(fkey.DependentToPrincipal.Name) };
}
return null;
}
public override PropertyInfo GetInverseProperty(PropertyInfo propertyInfo)
{
foreach (IEntityType efEntityType in GetEntityTypes(propertyInfo))
{
foreach (IForeignKey fkey in efEntityType.GetForeignKeys())
if (fkey.DependentToPrincipal.Name == propertyInfo.Name)
{
INavigation inverseProperty = fkey.DependentToPrincipal.FindInverse();
return inverseProperty?.DeclaringEntityType.ClrType.GetPropertyIgnoreCase(inverseProperty.Name);
}
foreach (IForeignKey fkey in efEntityType.GetReferencingForeignKeys())
if (fkey.PrincipalToDependent != null && fkey.PrincipalToDependent.Name == propertyInfo.Name)
{
INavigation inverseProperty = fkey.PrincipalToDependent.FindInverse();
return inverseProperty?.DeclaringEntityType.ClrType.GetPropertyIgnoreCase(inverseProperty.Name);
}
}
return null;
}
public override int GetOrder(PropertyInfo propertyInfo)
{
foreach (IEntityType efEntityType in GetEntityTypes(propertyInfo))
{
foreach (IKey key in efEntityType.GetKeys())
for (int i = 0; i < key.Properties.Count; i++)
if (key.Properties[i].Name == propertyInfo.Name)
return i;
foreach (IForeignKey fkey in efEntityType.GetForeignKeys())
for (int i = 0; i < fkey.Properties.Count; i++)
if (fkey.Properties[i].Name == propertyInfo.Name)
return i;
}
return -1;
}
public override bool IsKey(PropertyInfo propertyInfo)
{
foreach (IEntityType efEntityType in GetEntityTypes(propertyInfo))
foreach (IKey key in efEntityType.GetKeys())
for (int i = 0; i < key.Properties.Count; i++)
if (key.Properties[i].Name == propertyInfo.Name)
return true;
return false;
}
public override bool IsNotMapped(PropertyInfo propertyInfo)
{
foreach (IEntityType efEntityType in GetEntityTypes(propertyInfo))
{
foreach (IProperty efProperty in efEntityType.GetProperties())
if (efProperty.Name == propertyInfo.Name)
return false;
foreach (IForeignKey fkey in efEntityType.GetForeignKeys())
if (fkey.DependentToPrincipal.Name == propertyInfo.Name)
return false;
foreach (IForeignKey fkey in efEntityType.GetReferencingForeignKeys())
if (fkey.PrincipalToDependent.Name == propertyInfo.Name)
return false;
}
return true;
}
public override bool IsRequired(PropertyInfo propertyInfo)
{
foreach (IEntityType efEntityType in GetEntityTypes(propertyInfo))
{
foreach (IProperty efProperty in efEntityType.GetProperties())
if (efProperty.Name == propertyInfo.Name)
return !efProperty.IsNullable;
foreach (IForeignKey fkey in efEntityType.GetForeignKeys())
if (fkey.DependentToPrincipal.Name == propertyInfo.Name)
return fkey.IsRequired;
foreach (IForeignKey fkey in efEntityType.GetReferencingForeignKeys())
if (fkey.PrincipalToDependent.Name == propertyInfo.Name)
return fkey.IsRequired;
}
throw new InvalidOperationException("property " + propertyInfo.Name + " not found");
}
}
}