Skip to content

Commit

Permalink
add rowversion generation to entity type config (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony authored Sep 18, 2024
1 parent fada948 commit c4dd307
Showing 1 changed file with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ protected override MethodDeclarationSyntax[] GetMethodDeclarations(EntityFramewo
}
}

if (entityGenerationModel.GenerateRowVersionColumn)
{
result.Add(GetRowVersionMappingMethodDeclaration(entityName));
}

if (entityGenerationModel.ChildEntityRelationships != null)
{
foreach (var childEntityRelationship in entityGenerationModel.ChildEntityRelationships)
Expand Down Expand Up @@ -289,17 +294,37 @@ private MethodDeclarationSyntax GetIdMethodDeclaration(string entityName)
return declaration;
}

private MethodDeclarationSyntax GetRowVersionMappingMethodDeclaration(string entityName)
{
var propertyInvocation = GetRowVersionEfPropertyInvocation();
return GetPropertyMappingMethodDeclaration(
entityName,
"RowVersion",
propertyInvocation);
}

private MethodDeclarationSyntax GetPropertyMappingMethodDeclaration(
string entityName,
PropertyInfoBase propertyInfoBase)
{
var methodName = $"Configure{propertyInfoBase.Name}Column";
var propertyInvocation = GetEfPropertyInvocation(propertyInfoBase);
return GetPropertyMappingMethodDeclaration(
entityName,
propertyInfoBase.Name,
propertyInvocation);
}

var body = new List<StatementSyntax>();
private MethodDeclarationSyntax GetPropertyMappingMethodDeclaration(
string entityName,
string propertyName,
StatementSyntax efPropertyInvocationStatementSyntax)
{
var methodName = $"Configure{propertyName}Column";

// get the initial property method invoke to use as the basis of chaining others together
var propertyInvocation = GetEfPropertyInvocation(propertyInfoBase);
body.Add(propertyInvocation);
var body = new List<StatementSyntax> {
efPropertyInvocationStatementSyntax
};


var parameters = GetParams(new[] { $"Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder<EfModels.{entityName}EfModel> builder" });
Expand Down Expand Up @@ -389,6 +414,22 @@ private StatementSyntax GetEfParentRelationshipInvocation(
return SyntaxFactory.ExpressionStatement(fluentApiInvocation);
}

private StatementSyntax GetRowVersionEfPropertyInvocation()
{
var fluentApiInvocation = RoslynGenerationHelpers.GetMethodOnVariableInvocationExpression(
"builder",
"Property",
new[] { $"table => table.RowVersion" },
false);

fluentApiInvocation = RoslynGenerationHelpers.GetFluentApiChainedInvocationExpression(
fluentApiInvocation,
"IsRowVersion",
null);

return SyntaxFactory.ExpressionStatement(fluentApiInvocation);
}

private StatementSyntax GetEfPropertyInvocation(PropertyInfoBase propertyInfoBase)
{
var fluentApiInvocation = RoslynGenerationHelpers.GetMethodOnVariableInvocationExpression(
Expand Down

0 comments on commit c4dd307

Please sign in to comment.