Skip to content

Commit 271f0cd

Browse files
authored
Merge pull request #620 from DavidBoone/parseCompositeForeignKeys
Add support for composite foreign keys to Parsing/MappingVisitor
2 parents f8096d1 + e0e9695 commit 271f0cd

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/EntityFrameworkCore.Generator.Core/Parsing/MappingVisitor.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,46 @@ private static string ParseMethodName(InvocationExpressionSyntax node)
114114
return propertyName;
115115
}
116116

117+
private List<string> ParseLambdaExpressionForAnonymousObject(InvocationExpressionSyntax node)
118+
{
119+
if (node == null)
120+
return null;
121+
122+
var lambdaExpression = node
123+
.ArgumentList
124+
.DescendantNodes()
125+
.OfType<LambdaExpressionSyntax>()
126+
.FirstOrDefault();
127+
128+
if (lambdaExpression == null)
129+
return null;
130+
131+
var anonymousObject = lambdaExpression
132+
.ChildNodes()
133+
.OfType<AnonymousObjectCreationExpressionSyntax>()
134+
.FirstOrDefault();
135+
136+
if (anonymousObject == null)
137+
return null;
138+
139+
var propertyNames = anonymousObject
140+
.ChildNodes()
141+
.OfType<AnonymousObjectMemberDeclaratorSyntax>()
142+
.Select(declarator => declarator
143+
.ChildNodes()
144+
.OfType<MemberAccessExpressionSyntax>()
145+
.FirstOrDefault())
146+
.Where(memberAccess => memberAccess != null)
147+
.Select(memberAccess => memberAccess
148+
.ChildNodes()
149+
.OfType<IdentifierNameSyntax>()
150+
.Select(identifier => identifier.Identifier.ValueText)
151+
.LastOrDefault())
152+
.Where(propertyName => propertyName != null)
153+
.ToList();
154+
155+
return propertyNames;
156+
}
117157

118158
private void ParseHasOne(InvocationExpressionSyntax node)
119159
{
@@ -151,13 +191,17 @@ private void ParseForeignKey(InvocationExpressionSyntax node)
151191
if (node == null || ParsedEntity == null)
152192
return;
153193

154-
var propertyName = ParseLambaExpression(node);
194+
List<string> propertyNames = null;
195+
if (ParseLambaExpression(node) is string propertyName && !string.IsNullOrEmpty(propertyName))
196+
propertyNames = new List<string> { propertyName };
197+
else
198+
propertyNames = ParseLambdaExpressionForAnonymousObject(node);
155199

156-
if (string.IsNullOrEmpty(propertyName))
200+
if (propertyNames == null)
157201
return;
158202

159203
_currentRelationship ??= new ParsedRelationship();
160-
_currentRelationship.Properties.Add(propertyName);
204+
_currentRelationship.Properties.AddRange(propertyNames);
161205
}
162206

163207
private void ParseConstraintName(InvocationExpressionSyntax node)

0 commit comments

Comments
 (0)