Link Cards to Accounts with proper relationship

Model Changes:
- Add Card.AccountId (nullable FK to Account)
- Add Card.Nickname field for friendly names
- Add Card.DisplayLabel computed property
- Add Account.Cards navigation property
- Add Account.DisplayLabel computed property

DbContext Updates:
- Configure Card → Account relationship (optional, restrict delete)
- Add index on Card.AccountId
- Set Card.Owner as required

Migration:
- Add LinkCardsToAccounts migration
- Adds AccountId and Nickname columns to Cards table
- Creates FK constraint from Cards to Accounts

This properly models the real-world relationship where payment cards
are linked to bank accounts (e.g., a Visa card draws from a checking
account, or a credit card is paid from a savings account).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
AJ
2025-10-09 22:56:11 -04:00
parent a44b3d41ac
commit ea8d1b442a
6 changed files with 646 additions and 3 deletions

View File

@@ -27,8 +27,18 @@ namespace MoneyMap.Data
{
e.Property(x => x.Issuer).HasMaxLength(100).IsRequired();
e.Property(x => x.Last4).HasMaxLength(4).IsRequired();
e.Property(x => x.Owner).HasMaxLength(100);
e.Property(x => x.Owner).HasMaxLength(100).IsRequired();
e.Property(x => x.Nickname).HasMaxLength(50);
// Card can be linked to an account (optional - for credit cards without linked account)
e.HasOne(x => x.Account)
.WithMany(a => a.Cards)
.HasForeignKey(x => x.AccountId)
.OnDelete(DeleteBehavior.Restrict)
.IsRequired(false);
e.HasIndex(x => new { x.Issuer, x.Last4, x.Owner });
e.HasIndex(x => x.AccountId);
});
// ---------- ACCOUNT ----------