🐛 Fix account profile relationship

This commit is contained in:
LittleSheep 2025-05-22 02:12:48 +08:00
parent aa0d2ab3c4
commit 8e8a120a90
5 changed files with 3517 additions and 19 deletions

View File

@ -54,16 +54,13 @@ public class AccountService(
public async Task EnsureAccountProfileCreated()
{
var accountsId = await db.Accounts.Select(a => a.Id).ToListAsync();
var missingId = await db.AccountProfiles
.IgnoreAutoIncludes()
.Where(p => !accountsId.Contains(p.AccountId))
.Select(p => p.AccountId)
.ToListAsync();
var existingId = await db.AccountProfiles.Select(p => p.AccountId).ToListAsync();
var missingId = accountsId.Except(existingId).ToList();
if (missingId.Count != 0)
{
var newProfiles = missingId.Select(id => new Profile { AccountId = id }).ToList();
await db.BulkInsertAsync(newProfiles, config => config.ConflictOption = ConflictOption.Ignore);
var newProfiles = missingId.Select(id => new Profile { Id = Guid.NewGuid(), AccountId = id }).ToList();
await db.BulkInsertAsync(newProfiles);
}
}
}

View File

@ -140,11 +140,6 @@ public class AppDatabase(
.HasForeignKey(pg => pg.GroupId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Account.Account>()
.HasOne(a => a.Profile)
.WithOne(p => p.Account)
.HasForeignKey<Account.Profile>(p => p.Id);
modelBuilder.Entity<Account.Relationship>()
.HasKey(r => new { FromAccountId = r.AccountId, ToAccountId = r.RelatedId });
modelBuilder.Entity<Account.Relationship>()

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,52 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DysonNetwork.Sphere.Migrations
{
/// <inheritdoc />
public partial class FixProfileRelationship : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "fk_account_profiles_accounts_id",
table: "account_profiles");
migrationBuilder.CreateIndex(
name: "ix_account_profiles_account_id",
table: "account_profiles",
column: "account_id",
unique: true);
migrationBuilder.AddForeignKey(
name: "fk_account_profiles_accounts_account_id",
table: "account_profiles",
column: "account_id",
principalTable: "accounts",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "fk_account_profiles_accounts_account_id",
table: "account_profiles");
migrationBuilder.DropIndex(
name: "ix_account_profiles_account_id",
table: "account_profiles");
migrationBuilder.AddForeignKey(
name: "fk_account_profiles_accounts_id",
table: "account_profiles",
column: "id",
principalTable: "accounts",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
}
}
}

View File

@ -529,6 +529,7 @@ namespace DysonNetwork.Sphere.Migrations
modelBuilder.Entity("DysonNetwork.Sphere.Account.Profile", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasColumnName("id");
@ -603,6 +604,10 @@ namespace DysonNetwork.Sphere.Migrations
b.HasKey("Id")
.HasName("pk_account_profiles");
b.HasIndex("AccountId")
.IsUnique()
.HasDatabaseName("ix_account_profiles_account_id");
b.HasIndex("BackgroundId")
.HasDatabaseName("ix_account_profiles_background_id");
@ -2699,18 +2704,18 @@ namespace DysonNetwork.Sphere.Migrations
modelBuilder.Entity("DysonNetwork.Sphere.Account.Profile", b =>
{
b.HasOne("DysonNetwork.Sphere.Account.Account", "Account")
.WithOne("Profile")
.HasForeignKey("DysonNetwork.Sphere.Account.Profile", "AccountId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
.HasConstraintName("fk_account_profiles_accounts_account_id");
b.HasOne("DysonNetwork.Sphere.Storage.CloudFile", "Background")
.WithMany()
.HasForeignKey("BackgroundId")
.HasConstraintName("fk_account_profiles_files_background_id");
b.HasOne("DysonNetwork.Sphere.Account.Account", "Account")
.WithOne("Profile")
.HasForeignKey("DysonNetwork.Sphere.Account.Profile", "Id")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
.HasConstraintName("fk_account_profiles_accounts_id");
b.HasOne("DysonNetwork.Sphere.Storage.CloudFile", "Picture")
.WithMany()
.HasForeignKey("PictureId")