Added n-m relation mapping

This commit is contained in:
2025-01-28 12:09:16 +01:00
parent 62e4daf60d
commit c8a342986b
5 changed files with 62 additions and 14 deletions

View File

@@ -11,7 +11,15 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="0648788e-7696-4e60-bf12-5d5601f33d8c" name="Changes" comment="Added maximum display length" /> <list default="true" id="0648788e-7696-4e60-bf12-5d5601f33d8c" name="Changes" comment="Fixed test for table view">
<change beforePath="$PROJECT_DIR$/.idea/.idea.HopFrame/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.HopFrame/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Config/PropertyConfig.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Config/PropertyConfig.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Services/Implementations/ContextExplorer.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Services/Implementations/ContextExplorer.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/testing/HopFrame.Testing/Components/Pages/Home.razor" beforeDir="false" afterPath="$PROJECT_DIR$/testing/HopFrame.Testing/Components/Pages/Home.razor" afterDir="false" />
<change beforePath="$PROJECT_DIR$/testing/HopFrame.Testing/DatabaseContext.cs" beforeDir="false" afterPath="$PROJECT_DIR$/testing/HopFrame.Testing/DatabaseContext.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/testing/HopFrame.Testing/Models/Post.cs" beforeDir="false" afterPath="$PROJECT_DIR$/testing/HopFrame.Testing/Models/Post.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/testing/HopFrame.Testing/Program.cs" beforeDir="false" afterPath="$PROJECT_DIR$/testing/HopFrame.Testing/Program.cs" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@@ -30,7 +38,7 @@
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_BRANCH_BY_REPOSITORY"> <option name="RECENT_BRANCH_BY_REPOSITORY">
<map> <map>
<entry key="$PROJECT_DIR$" value="feature/async-delegates" /> <entry key="$PROJECT_DIR$" value="feature/max-length" />
</map> </map>
</option> </option>
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
@@ -99,7 +107,7 @@
"RunOnceActivity.git.unshallow": "true", "RunOnceActivity.git.unshallow": "true",
"b5f11219-dfc4-47a1-b02c-90ab603034fb.executor": "Debug", "b5f11219-dfc4-47a1-b02c-90ab603034fb.executor": "Debug",
"dcdf1689-dc07-47e4-8824-2e60a4fbf301.executor": "Debug", "dcdf1689-dc07-47e4-8824-2e60a4fbf301.executor": "Debug",
"git-widget-placeholder": "!23 on feature/max-length", "git-widget-placeholder": "!24 on fix/relations",
"list.type.of.created.stylesheet": "CSS", "list.type.of.created.stylesheet": "CSS",
"node.js.detected.package.eslint": "true", "node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true", "node.js.detected.package.tslint": "true",
@@ -205,7 +213,7 @@
<workItem from="1737390240714" duration="60000" /> <workItem from="1737390240714" duration="60000" />
<workItem from="1737390360987" duration="601000" /> <workItem from="1737390360987" duration="601000" />
<workItem from="1737993570961" duration="4163000" /> <workItem from="1737993570961" duration="4163000" />
<workItem from="1738054766160" duration="741000" /> <workItem from="1738054766160" duration="6716000" />
</task> </task>
<task id="LOCAL-00001" summary="Added basic configuration"> <task id="LOCAL-00001" summary="Added basic configuration">
<option name="closed" value="true" /> <option name="closed" value="true" />
@@ -415,7 +423,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1738055497527</updated> <updated>1738055497527</updated>
</task> </task>
<option name="localTasksCounter" value="27" /> <task id="LOCAL-00027" summary="Fixed test for table view">
<option name="closed" value="true" />
<created>1738055822074</created>
<option name="number" value="00027" />
<option name="presentableId" value="LOCAL-00027" />
<option name="project" value="LOCAL" />
<updated>1738055822074</updated>
</task>
<option name="localTasksCounter" value="28" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@@ -466,7 +482,6 @@
<component name="UnityProjectConfiguration" hasMinimizedUI="false" /> <component name="UnityProjectConfiguration" hasMinimizedUI="false" />
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" /> <option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
<MESSAGE value="Added admin page navigation" />
<MESSAGE value="Added database loading logic" /> <MESSAGE value="Added database loading logic" />
<MESSAGE value="Started working on listing page" /> <MESSAGE value="Started working on listing page" />
<MESSAGE value="Added entry saving support" /> <MESSAGE value="Added entry saving support" />
@@ -491,6 +506,7 @@
<MESSAGE value="Added a simple web api abstraction method" /> <MESSAGE value="Added a simple web api abstraction method" />
<MESSAGE value="Implemented async delegates" /> <MESSAGE value="Implemented async delegates" />
<MESSAGE value="Added maximum display length" /> <MESSAGE value="Added maximum display length" />
<option name="LAST_COMMIT_MESSAGE" value="Added maximum display length" /> <MESSAGE value="Fixed test for table view" />
<option name="LAST_COMMIT_MESSAGE" value="Fixed test for table view" />
</component> </component>
</project> </project>

View File

@@ -200,4 +200,16 @@ public class PropertyConfigurator<TProp>(PropertyConfig config) {
InnerConfig.DisplayLength = maxLength; InnerConfig.DisplayLength = maxLength;
return this; return this;
} }
/// <summary>
/// Forces a property to be treated as a relation
/// </summary>
/// <param name="isEnumerable">Determines if it is possible to assign multiple objects to the property</param>
/// <param name="isRequired">Determines if the property is nullable</param>
public PropertyConfigurator<TProp> ForceRelation(bool isEnumerable = false, bool isRequired = true) {
InnerConfig.IsRelation = true;
InnerConfig.IsEnumerable = isEnumerable;
InnerConfig.IsRequired = isRequired;
return this;
}
} }

View File

@@ -1,6 +1,7 @@
using System.Text.Json; using System.Text.Json;
using HopFrame.Core.Config; using HopFrame.Core.Config;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace HopFrame.Core.Services.Implementations; namespace HopFrame.Core.Services.Implementations;
@@ -56,15 +57,35 @@ internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider pr
private void SeedTableData(TableConfig table) { private void SeedTableData(TableConfig table) {
if (table.Seeded) return; if (table.Seeded) return;
var dbContext = (provider.GetService(table.ContextConfig.ContextType) as DbContext)!; var dbContext = (provider.GetRequiredService(table.ContextConfig.ContextType) as DbContext)!;
var entity = dbContext.Model.FindEntityType(table.TableType)!; var entity = dbContext.Model.FindEntityType(table.TableType)!;
foreach (var propertyConfig in table.Properties) { foreach (var propertyConfig in table.Properties) {
if (propertyConfig.IsListingProperty) continue; if (propertyConfig.IsListingProperty) continue;
if (propertyConfig.IsRelation) continue;
var prop = entity.FindProperty(propertyConfig.Info.Name); var prop = entity.FindProperty(propertyConfig.Info.Name);
if (prop is not null) continue; if (prop is not null) continue;
var nav = entity.FindNavigation(propertyConfig.Info.Name); var nav = entity.FindNavigation(propertyConfig.Info.Name);
if (nav is null) continue; if (nav is null) {
if (!propertyConfig.Info.PropertyType.IsGenericType) continue;
var relationType = propertyConfig.Info.PropertyType.GenericTypeArguments[0];
var isRelation = entity.Model.GetEntityTypes()
.Select(e => e.GetForeignKeys())
.Any(keys => keys
.Select(k => k.PrincipalEntityType.ClrType)
.Any(t => t == relationType || t == table.TableType));
if (!isRelation) continue;
propertyConfig.IsRelation = true;
propertyConfig.IsRequired = false;
propertyConfig.IsEnumerable = true;
continue;
}
propertyConfig.IsRelation = true; propertyConfig.IsRelation = true;
propertyConfig.IsRequired = nav.ForeignKey.IsRequired; propertyConfig.IsRequired = nav.ForeignKey.IsRequired;
propertyConfig.IsEnumerable = nav.IsCollection; propertyConfig.IsEnumerable = nav.IsCollection;
@@ -74,7 +95,7 @@ internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider pr
var propConfig = table.Properties var propConfig = table.Properties
.Where(prop => !prop.IsListingProperty) .Where(prop => !prop.IsListingProperty)
.SingleOrDefault(prop => prop.Info == property.PropertyInfo); .SingleOrDefault(prop => prop.Info == property.PropertyInfo);
if (propConfig is null) continue; if (propConfig is null || propConfig.IsRequired) continue;
propConfig.IsRequired = !property.IsNullable; propConfig.IsRequired = !property.IsNullable;
} }

View File

@@ -12,8 +12,7 @@ public class DatabaseContext(DbContextOptions<DatabaseContext> options) : DbCont
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Post>() modelBuilder.Entity<Post>()
.HasOne<User>(p => p.Author) .HasOne(p => p.Author)
.WithMany(u => u.Posts) .WithMany(u => u.Posts);
.OnDelete(DeleteBehavior.Cascade);
} }
} }

View File

@@ -95,6 +95,6 @@ app.UseAntiforgery();
app.MapStaticAssets(); app.MapStaticAssets();
app.MapRazorComponents<App>() app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode() .AddInteractiveServerRenderMode()
.MapHopFramePages(); .AddHopFramePages();
app.Run(); app.Run();