-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathForeignKeyAttributeConvention.cs
More file actions
146 lines (127 loc) · 6.18 KB
/
ForeignKeyAttributeConvention.cs
File metadata and controls
146 lines (127 loc) · 6.18 KB
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
141
142
143
144
145
146
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.AspNet.OData.Common;
using Microsoft.OData.Edm;
namespace Microsoft.AspNet.OData.Builder.Conventions.Attributes
{
// Denotes a property used as a foreign key in a relationship. The annotation may be placed on:
// 1. the foreign key property and specify the associated navigation property name, or
// 2. a navigation property and specify the associated foreign key name.
internal class ForeignKeyAttributeConvention : AttributeEdmPropertyConvention<PropertyConfiguration>
{
public ForeignKeyAttributeConvention()
: base(attribute => attribute.GetType() == typeof(ForeignKeyAttribute), allowMultiple: false)
{
}
/// <inheritdoc/>
public override void Apply(PropertyConfiguration edmProperty,
StructuralTypeConfiguration structuralTypeConfiguration, Attribute attribute,
ODataConventionModelBuilder model)
{
if (edmProperty == null)
{
throw Error.ArgumentNull("edmProperty");
}
if (structuralTypeConfiguration == null)
{
throw Error.ArgumentNull("structuralTypeConfiguration");
}
if (attribute == null)
{
throw Error.ArgumentNull("attribute");
}
EntityTypeConfiguration declaringEntityType = structuralTypeConfiguration as EntityTypeConfiguration;
if (declaringEntityType == null)
{
return;
}
ForeignKeyAttribute foreignKeyAttribute = (ForeignKeyAttribute)attribute;
switch (edmProperty.Kind)
{
case PropertyKind.Navigation:
ApplyNavigation((NavigationPropertyConfiguration)edmProperty, declaringEntityType,
foreignKeyAttribute);
break;
case PropertyKind.Primitive:
ApplyPrimitive((PrimitivePropertyConfiguration)edmProperty, declaringEntityType,
foreignKeyAttribute);
break;
}
}
private static void ApplyNavigation(NavigationPropertyConfiguration navProperty, EntityTypeConfiguration entityType,
ForeignKeyAttribute foreignKeyAttribute)
{
Contract.Assert(navProperty != null);
Contract.Assert(entityType != null);
Contract.Assert(foreignKeyAttribute != null);
if (navProperty.AddedExplicitly || navProperty.Multiplicity == EdmMultiplicity.Many)
{
return;
}
EntityTypeConfiguration principalEntity = entityType.ModelBuilder.StructuralTypes
.OfType<EntityTypeConfiguration>().FirstOrDefault(e => e.ClrType == navProperty.RelatedClrType);
if (principalEntity == null)
{
return;
}
// if a navigation property has multiple foreign keys, use comma to separate the list of foreign key names.
IEnumerable<string> dependentPropertyNames = foreignKeyAttribute.Name.Split(',').Select(p => p.Trim());
foreach (string dependentPropertyName in dependentPropertyNames)
{
if (String.IsNullOrWhiteSpace(dependentPropertyName))
{
continue;
}
PrimitivePropertyConfiguration dependent =
entityType.Properties.OfType<PrimitivePropertyConfiguration>()
.SingleOrDefault(p => p.Name.Equals(dependentPropertyName, StringComparison.Ordinal));
if (dependent != null)
{
Type dependentType = Nullable.GetUnderlyingType(dependent.PropertyInfo.PropertyType) ?? dependent.PropertyInfo.PropertyType;
PrimitivePropertyConfiguration principal = principalEntity.Keys.FirstOrDefault(
k => k.PropertyInfo.PropertyType == dependentType && navProperty.PrincipalProperties.All(p => p != k.PropertyInfo.MemberInfo));
if (principal != null)
{
navProperty.HasConstraint(dependent.PropertyInfo, principal.PropertyInfo);
}
}
}
}
private static void ApplyPrimitive(PrimitivePropertyConfiguration dependent, EntityTypeConfiguration entityType,
ForeignKeyAttribute foreignKeyAttribute)
{
Contract.Assert(dependent != null);
Contract.Assert(entityType != null);
Contract.Assert(foreignKeyAttribute != null);
string navName = foreignKeyAttribute.Name.Trim();
NavigationPropertyConfiguration navProperty = entityType.NavigationProperties
.FirstOrDefault(n => n.Name.Equals(navName, StringComparison.Ordinal));
if (navProperty == null)
{
return;
}
if (navProperty.Multiplicity == EdmMultiplicity.Many || navProperty.AddedExplicitly)
{
return;
}
EntityTypeConfiguration principalEntity = entityType.ModelBuilder.StructuralTypes
.OfType<EntityTypeConfiguration>().FirstOrDefault(e => e.ClrType == navProperty.RelatedClrType);
if (principalEntity == null)
{
return;
}
Type dependentType = Nullable.GetUnderlyingType(dependent.PropertyInfo.PropertyType) ?? dependent.PropertyInfo.PropertyType;
PrimitivePropertyConfiguration principal = principalEntity.Keys.FirstOrDefault(
k => k.PropertyInfo.PropertyType == dependentType && navProperty.PrincipalProperties.All(p => p != k.PropertyInfo.MemberInfo));
if (principal != null)
{
navProperty.HasConstraint(dependent.PropertyInfo, principal.PropertyInfo);
}
}
}
}