Skip to content

Commit 4cacc95

Browse files
Made Auto-generated for EF also call save changes
1 parent a65d70e commit 4cacc95

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

Modules/Intent.Modules.Application.DomainInteractions/Intent.Application.DomainInteractions.imodspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<package>
33
<id>Intent.Application.DomainInteractions</id>
4-
<version>1.1.6</version>
4+
<version>1.1.7-pre.0</version>
55
<supportedClientVersions>[4.5.15-a,5.0.0)</supportedClientVersions>
66
<summary>Provides interaction strategies to generate domain interaction implementations.</summary>
77
<description>Provides interaction strategies to generate domain interaction implementations.</description>

Modules/Intent.Modules.Application.DomainInteractions/Intent.Modules.Application.DomainInteractions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
5-
<Version>1.1.6</Version>
5+
<Version>1.1.7-pre.0</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

Modules/Intent.Modules.Application.DomainInteractions/InteractionStrategies/CreateEntityInteractionStrategy.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private static void AddSaveChangesStatements(ICSharpClassMethodDeclaration metho
9898
if (entity.ElementModel.IsClassModel())
9999
{
100100
var primaryKeys = entity.ElementModel.AsClassModel().GetTypesInHierarchy().SelectMany(c => c.Attributes.Where(a => a.IsPrimaryKey()));
101-
if (primaryKeys.Any(HasDBGeneratedPk))
101+
if (primaryKeys.Any(p => HasDBGeneratedPk(entity.ElementModel, p)))
102102
{
103103
method.AddStatement(ExecutionPhases.Persistence, new CSharpStatement($"{entity.DataAccessProvider.SaveChangesAsync()}"));
104104
}
@@ -137,9 +137,34 @@ private static List<EntityDetails> GetEntitiesReturningPk(ICSharpClassMethodDecl
137137
.ToList();
138138
}
139139

140-
private static bool HasDBGeneratedPk(AttributeModel attribute)
140+
private class DataSources
141141
{
142-
return attribute.IsPrimaryKey() && attribute.GetStereotypeProperty("Primary Key", "Data source", "Default") == "Default";
142+
/// <summary>
143+
/// Explicitly Stating the Keys are Auto-generated
144+
/// DB Tech may have options for how ths is done, e.g. EF has options here
145+
/// For EF this typically means assigned on Save Changes
146+
/// For Cosmos this would typically be set on object construction
147+
/// </summary>
148+
public const string AutoGenerated = "Auto-generated";
149+
/// <summary>
150+
/// What ever the default is for the technology.
151+
/// e.g. EF Auto-generated PKs, Guids EF assigned in Memory, Oridinals are DB Assigned
152+
/// </summary>
153+
public const string Default = "Default";
154+
/// <summary>
155+
/// PKS are set but the explicitly (Business keys, composite keys, these are typcially not auto assigned)
156+
/// </summary>
157+
public const string UserSupplied = "User supplied";
158+
}
159+
160+
private static bool HasDBGeneratedPk(IElement entity, AttributeModel attribute)
161+
{
162+
if (entity.Package.AsDomainPackageModel()?.HasStereotype("Relational Database") == true)
163+
{
164+
return attribute.IsPrimaryKey() && attribute.GetStereotypeProperty("Primary Key", "Data source", "Default") is DataSources.Default or DataSources.AutoGenerated;
165+
}
166+
// For document DBs User Supplied and Auto-generated is assumed to not be set by the DB
167+
return attribute.IsPrimaryKey() && attribute.GetStereotypeProperty("Primary Key", "Data source", "Default") == DataSources.Default;
143168
}
144169
}
145170
}

Modules/Intent.Modules.Application.DomainInteractions/release-notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### Version 1.1.7
2+
3+
- Fixed: CRUD not explicitly calls SaveChanges for EF when modeling Auto-generated primary keys.
4+
15
### Version 1.1.6
26

37
- Improvement: Improved error message when `ProjectTo` is selected as the query strategy with an unsupported provider (Mapperly).

0 commit comments

Comments
 (0)