Scaffolding does not creating the FK with AspNetUser (Identity)

Viewed 55

Hello I have been able to generate the scaffolding and it is properly working but I have a column in a custom table that should be related with the AspNetUser (identity) table. When I run the command to scaffold the models and ApplicationDbContext I have a warning message:

Skipping foreign key 'FK_USER' on table 'dbo.DCPlayer' since principal table 'dbo.AspNetUsers' was not found in the model. This usually happens when the principal table was not included in the selection set.

This is the ApplicationDbContext:

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace Darkcrow_Dashboard.Models
{
    public partial class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
        {
        }

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Artifact> Artifacts { get; set; } = null!;
        public virtual DbSet<ArtifactSize> ArtifactSizes { get; set; } = null!;
        public virtual DbSet<Dcplayer> Dcplayers { get; set; } = null!;
        public virtual DbSet<Dcvillage> Dcvillages { get; set; } = null!;
        public virtual DbSet<DefensiveForm> DefensiveForms { get; set; } = null!;
        public virtual DbSet<OtherPlayer> OtherPlayers { get; set; } = null!;
        public virtual DbSet<OtherVillage> OtherVillages { get; set; } = null!;
        public virtual DbSet<Vagon> Vagons { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Server=localhost\\SQLEXPRESS;Database=DarkCrow;Trusted_Connection=True;");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Artifact>(entity =>
            {
                entity.HasKey(e => e.ArtifactName);

                entity.ToTable("Artifact");

                entity.Property(e => e.ArtifactName)
                    .HasMaxLength(45)
                    .IsUnicode(false);

                entity.HasMany(d => d.Sizes)
                    .WithMany(p => p.ArtifactNames)
                    .UsingEntity<Dictionary<string, object>>(
                        "ArtifactArtifactSize",
                        l => l.HasOne<ArtifactSize>().WithMany().HasForeignKey("Size").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_Multi_ArtifactSize_Artifact"),
                        r => r.HasOne<Artifact>().WithMany().HasForeignKey("ArtifactName").OnDelete(DeleteBehavior.ClientSetNull).HasConstraintName("FK_Multi_Artifact_ArtifactSize"),
                        j =>
                        {
                            j.HasKey("ArtifactName", "Size");

                            j.ToTable("Artifact_ArtifactSize");

                            j.IndexerProperty<string>("ArtifactName").HasMaxLength(45).IsUnicode(false);

                            j.IndexerProperty<string>("Size").HasMaxLength(10).IsUnicode(false);
                        });
            });

            modelBuilder.Entity<ArtifactSize>(entity =>
            {
                entity.HasKey(e => e.Size);

                entity.ToTable("ArtifactSize");

                entity.Property(e => e.Size)
                    .HasMaxLength(10)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<Dcplayer>(entity =>
            {
                entity.ToTable("DCPlayer");

                entity.HasIndex(e => e.Username, "UQ__DCPlayer__536C85E49AD1C079")
                    .IsUnique();

                entity.Property(e => e.Id)
                    .ValueGeneratedNever()
                    .HasColumnName("ID");

                entity.Property(e => e.UserIdentityId)
                    .HasMaxLength(450)
                    .HasColumnName("UserIdentityID");

                entity.Property(e => e.Username)
                    .HasMaxLength(20)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<Dcvillage>(entity =>
            {
                entity.ToTable("DCVillage");

                entity.Property(e => e.Id)
                    .ValueGeneratedNever()
                    .HasColumnName("ID");

                entity.Property(e => e.DcplayerId).HasColumnName("DCPlayer_ID");

                entity.Property(e => e.VillageName)
                    .HasMaxLength(40)
                    .IsUnicode(false);

                entity.Property(e => e.Xcoordinate).HasColumnName("XCoordinate");

                entity.Property(e => e.Ycoordinate).HasColumnName("YCoordinate");

                entity.HasOne(d => d.Dcplayer)
                    .WithMany(p => p.Dcvillages)
                    .HasForeignKey(d => d.DcplayerId)
                    .HasConstraintName("FK_TEST");
            });

            modelBuilder.Entity<DefensiveForm>(entity =>
            {
                entity.HasKey(e => e.FormId)
                    .HasName("PK_formID");

                entity.ToTable("DefensiveForm");

                entity.Property(e => e.FormId).HasColumnName("formID");

                entity.Property(e => e.ArtifactName)
                    .HasMaxLength(45)
                    .IsUnicode(false)
                    .HasColumnName("Artifact_Name");

                entity.Property(e => e.CalculatedPt).HasColumnName("CalculatedPT");

                entity.Property(e => e.DcplayerId).HasColumnName("DCPlayer_ID");

                entity.Property(e => e.OtherPlayerId).HasColumnName("OtherPlayer_ID");

                entity.Property(e => e.Pt).HasColumnName("PT");

                entity.HasOne(d => d.ArtifactNameNavigation)
                    .WithMany(p => p.DefensiveForms)
                    .HasForeignKey(d => d.ArtifactName)
                    .OnDelete(DeleteBehavior.Cascade)
                    .HasConstraintName("FK_Artifact_DeffForm");

                entity.HasOne(d => d.Dcplayer)
                    .WithMany(p => p.DefensiveForms)
                    .HasForeignKey(d => d.DcplayerId)
                    .HasConstraintName("FK_DCPlayer_DeffForm");

                entity.HasOne(d => d.OtherPlayer)
                    .WithMany(p => p.DefensiveForms)
                    .HasForeignKey(d => d.OtherPlayerId)
                    .HasConstraintName("FK_OtherPlayer_DeffForm");
            });

            modelBuilder.Entity<OtherPlayer>(entity =>
            {
                entity.ToTable("OtherPlayer");

                entity.HasIndex(e => e.Username, "UQ__OtherPla__536C85E4DD06DE39")
                    .IsUnique();

                entity.Property(e => e.Id)
                    .ValueGeneratedNever()
                    .HasColumnName("ID");

                entity.Property(e => e.Alliance)
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.Username)
                    .HasMaxLength(20)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<OtherVillage>(entity =>
            {
                entity.ToTable("OtherVillage");

                entity.Property(e => e.Id)
                    .ValueGeneratedNever()
                    .HasColumnName("ID");

                entity.Property(e => e.OtherPlayerId).HasColumnName("OtherPlayer_ID");

                entity.Property(e => e.VillageName)
                    .HasMaxLength(40)
                    .IsUnicode(false);

                entity.Property(e => e.Xcoordinate).HasColumnName("XCoordinate");

                entity.Property(e => e.Ycoordinate).HasColumnName("YCoordinate");

                entity.HasOne(d => d.OtherPlayer)
                    .WithMany(p => p.OtherVillages)
                    .HasForeignKey(d => d.OtherPlayerId)
                    .HasConstraintName("FK_Village_OtherPlayer");
            });

            modelBuilder.Entity<Vagon>(entity =>
            {
                entity.ToTable("Vagon");

                entity.Property(e => e.Id).HasColumnName("ID");

                entity.Property(e => e.DefensiveFormId).HasColumnName("DefensiveForm_ID");

                entity.Property(e => e.VagonTime).HasColumnType("datetime");

                entity.HasOne(d => d.DefensiveForm)
                    .WithMany(p => p.Vagons)
                    .HasForeignKey(d => d.DefensiveFormId)
                    .HasConstraintName("FK_DeffForm_Vagon");
            });

            base.OnModelCreating(modelBuilder);
        }

    }
}

This is the part where I mention the userIdentityID:

                entity.Property(e => e.UserIdentityId)
                    .HasMaxLength(450)
                    .HasColumnName("UserIdentityID");

Is there any chance to hard code the FK and relationship with the table AspNetUser in a one-to-one relationship with my custom table DCPlayer?

1 Answers

You could create a custom class, say User class, that inherits from the IdentityUser.

in the dbcontext, you can add the custom class like bellow.

public partial class ApplicationDbContext : IdentityDbContext<User>
{
  ...

This will help you keep track of user context, and allow custom control. Just remember to make change in your startup.cs for bellow .Net Core 6 or program.cs in .Net core 6 and above.

Related