Goober
Goober

Reputation: 13508

Asp.Net Identity - Migration update results in error "There is already an object named 'AspNetRoles' in the database."

I have a fresh SQL Server database, hosted in Microsoft Azure.

Whenever I run the following commands in the Visual Studio for Mac terminal window:

dotnet ef migrations add "CreateProdAuthDatabase_PROD"
dotnet ef database update

I get the following error:

enter image description here

Why is this happening? I delete the ApplicationDbContextModelSnapshot file BEFORE I run the migration, and a fresh version of this file is generated, right at the point of me running the migration terminal command.

UPDATE Here is the generated snapshot file:

// <auto-generated />

using System; using FIRECalculator.Migrations; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace FIRECalculator.Migrations { [DbContext(typeof(ApplicationDbContext))] partial class ApplicationDbContextModelSnapshot : ModelSnapshot { protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder .HasAnnotation("ProductVersion", "8.0.1") .HasAnnotation("Relational:MaxIdentifierLength", 128);

        SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

        modelBuilder.Entity("FIRECalculator.Auth.Entities.ApplicationUser", b =>
            {
                b.Property<string>("Id")
                    .HasColumnType("nvarchar(450)");

                b.Property<int>("AccessFailedCount")
                    .HasColumnType("int");

                b.Property<DateTime?>("AccountUnlockedDateTimeUtc")
                    .HasColumnType("datetime2");

                b.Property<DateTime?>("BirthdayDateTime")
                    .HasColumnType("datetime2");

                b.Property<int?>("BirthdayDay")
                    .HasColumnType("int");

                b.Property<int?>("BirthdayMonth")
                    .HasColumnType("int");

                b.Property<int?>("BirthdayYear")
                    .HasColumnType("int");

                b.Property<string>("City")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("ConcurrencyStamp")
                    .IsConcurrencyToken()
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("Email")
                    .HasMaxLength(256)
                    .HasColumnType("nvarchar(256)");

                b.Property<bool>("EmailConfirmed")
                    .HasColumnType("bit");

                b.Property<DateTime?>("EmailConfirmedDateTimeUtc")
                    .HasColumnType("datetime2");

                b.Property<string>("FirstName")
                    .HasColumnType("nvarchar(max)");

                b.Property<bool?>("IsBanned")
                    .HasColumnType("bit");

                b.Property<bool?>("IsSubscribedToNewsletter")
                    .HasColumnType("bit");

                b.Property<string>("LastName")
                    .HasColumnType("nvarchar(max)");

                b.Property<bool>("LockoutEnabled")
                    .HasColumnType("bit");

                b.Property<DateTimeOffset?>("LockoutEnd")
                    .HasColumnType("datetimeoffset");

                b.Property<string>("NormalizedEmail")
                    .HasMaxLength(256)
                    .HasColumnType("nvarchar(256)");

                b.Property<string>("NormalizedUserName")
                    .HasMaxLength(256)
                    .HasColumnType("nvarchar(256)");

                b.Property<bool?>("OverrideAndIgnoreSubscriptionTier")
                    .HasColumnType("bit");

                b.Property<DateTime?>("OverrideAndIgnoreSubscriptionTierEndDateTimeUtc")
                    .HasColumnType("datetime2");

                b.Property<DateTime?>("OverrideAndIgnoreSubscriptionTierStartDateTimeUtc")
                    .HasColumnType("datetime2");

                b.Property<string>("PasswordHash")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("PhoneNumber")
                    .HasColumnType("nvarchar(max)");

                b.Property<bool>("PhoneNumberConfirmed")
                    .HasColumnType("bit");

                b.Property<byte[]>("ProfilePicture")
                    .HasColumnType("varbinary(max)");

                b.Property<string>("SecurityStamp")
                    .HasColumnType("nvarchar(max)");

                b.Property<DateTime?>("SubscriptionTierExpiresDateTimeUtc")
                    .HasColumnType("datetime2");

                b.Property<string>("SubscriptionTierFrequency")
                    .HasColumnType("nvarchar(max)");

                b.Property<DateTime?>("SubscriptionTierPaymentDateTimeUtc")
                    .HasColumnType("datetime2");

                b.Property<DateTime?>("SubscriptionTierStartDateTimeUtc")
                    .HasColumnType("datetime2");

                b.Property<string>("SubscriptionTierType")
                    .HasColumnType("nvarchar(max)");

                b.Property<bool>("TwoFactorEnabled")
                    .HasColumnType("bit");

                b.Property<bool?>("UserAccountIsLockedOut")
                    .HasColumnType("bit");

                b.Property<string>("UserName")
                    .HasMaxLength(256)
                    .HasColumnType("nvarchar(256)");

                b.HasKey("Id");

                b.HasIndex("NormalizedEmail")
                    .HasDatabaseName("EmailIndex");

                b.HasIndex("NormalizedUserName")
                    .IsUnique()
                    .HasDatabaseName("UserNameIndex")
                    .HasFilter("[NormalizedUserName] IS NOT NULL");

                b.ToTable("AspNetUsers", (string)null);
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
            {
                b.Property<string>("Id")
                    .HasColumnType("nvarchar(450)");

                b.Property<string>("ConcurrencyStamp")
                    .IsConcurrencyToken()
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("Name")
                    .HasMaxLength(256)
                    .HasColumnType("nvarchar(256)");

                b.Property<string>("NormalizedName")
                    .HasMaxLength(256)
                    .HasColumnType("nvarchar(256)");

                b.HasKey("Id");

                b.HasIndex("NormalizedName")
                    .IsUnique()
                    .HasDatabaseName("RoleNameIndex")
                    .HasFilter("[NormalizedName] IS NOT NULL");

                b.ToTable("AspNetRoles", (string)null);
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
            {
                b.Property<int>("Id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("int");

                SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

                b.Property<string>("ClaimType")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("ClaimValue")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("RoleId")
                    .IsRequired()
                    .HasColumnType("nvarchar(450)");

                b.HasKey("Id");

                b.HasIndex("RoleId");

                b.ToTable("AspNetRoleClaims", (string)null);
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
            {
                b.Property<int>("Id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("int");

                SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

                b.Property<string>("ClaimType")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("ClaimValue")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("UserId")
                    .IsRequired()
                    .HasColumnType("nvarchar(450)");

                b.HasKey("Id");

                b.HasIndex("UserId");

                b.ToTable("AspNetUserClaims", (string)null);
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
            {
                b.Property<string>("LoginProvider")
                    .HasColumnType("nvarchar(450)");

                b.Property<string>("ProviderKey")
                    .HasColumnType("nvarchar(450)");

                b.Property<string>("ProviderDisplayName")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("UserId")
                    .IsRequired()
                    .HasColumnType("nvarchar(450)");

                b.HasKey("LoginProvider", "ProviderKey");

                b.HasIndex("UserId");

                b.ToTable("AspNetUserLogins", (string)null);
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
            {
                b.Property<string>("UserId")
                    .HasColumnType("nvarchar(450)");

                b.Property<string>("RoleId")
                    .HasColumnType("nvarchar(450)");

                b.HasKey("UserId", "RoleId");

                b.HasIndex("RoleId");

                b.ToTable("AspNetUserRoles", (string)null);
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
            {
                b.Property<string>("UserId")
                    .HasColumnType("nvarchar(450)");

                b.Property<string>("LoginProvider")
                    .HasColumnType("nvarchar(450)");

                b.Property<string>("Name")
                    .HasColumnType("nvarchar(450)");

                b.Property<string>("Value")
                    .HasColumnType("nvarchar(max)");

                b.HasKey("UserId", "LoginProvider", "Name");

                b.ToTable("AspNetUserTokens", (string)null);
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
            {
                b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null)
                    .WithMany()
                    .HasForeignKey("RoleId")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
            {
                b.HasOne("FIRECalculator.Auth.Entities.ApplicationUser", null)
                    .WithMany()
                    .HasForeignKey("UserId")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
            {
                b.HasOne("FIRECalculator.Auth.Entities.ApplicationUser", null)
                    .WithMany()
                    .HasForeignKey("UserId")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
            {
                b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole", null)
                    .WithMany()
                    .HasForeignKey("RoleId")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();

                b.HasOne("FIRECalculator.Auth.Entities.ApplicationUser", null)
                    .WithMany()
                    .HasForeignKey("UserId")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();
            });

        modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
            {
                b.HasOne("FIRECalculator.Auth.Entities.ApplicationUser", null)
                    .WithMany()
                    .HasForeignKey("UserId")
                    .OnDelete(DeleteBehavior.Cascade)
                    .IsRequired();
            });

#pragma warning restore 612, 618 } } }

...and here is the ApplicationUser, which inherits from IdentityUser:

public sealed class ApplicationUser : IdentityUser

{ public string? FirstName { get; set; }

public string? LastName { get; set; }

public string? City { get; set; }

public byte[]? ProfilePicture { get; set; }

public DateTime? EmailConfirmedDateTimeUtc { get; set; }

public bool? UserAccountIsLockedOut { get; set; }

public DateTime? AccountUnlockedDateTimeUtc { get; set; }

public bool? IsBanned { get; set; }
public int? BirthdayDay { get; set; }
public int? BirthdayMonth { get; set; }
public int? BirthdayYear { get; set; }
public DateTime? BirthdayDateTime { get; set; }
public bool? IsSubscribedToNewsletter { get; set; }
public string? SubscriptionTierType { get; set; } //(FREE, Pro, Premium)
public DateTime? SubscriptionTierStartDateTimeUtc { get; set; }
public DateTime? SubscriptionTierExpiresDateTimeUtc { get; set; }
public DateTime? SubscriptionTierPaymentDateTimeUtc { get; set; }
public string? SubscriptionTierFrequency { get; set; } //(Monthly/Annual)
public bool? OverrideAndIgnoreSubscriptionTier { get; set; }
public DateTime? OverrideAndIgnoreSubscriptionTierStartDateTimeUtc { get; set; }
public DateTime? OverrideAndIgnoreSubscriptionTierEndDateTimeUtc { get; set; }

}

UPDATE 2:

Here is the ApplicationDbContext file:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

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

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    // Customize the ASP.NET Identity model and override the defaults if needed.
    // For example, you can rename the ASP.NET Identity table names and more.
    // Add your customizations after calling base.OnModelCreating(builder);
}

}

UPDATE 3:

Here is some key code in my Program.cs file:

builder.Services.AddIdentityCore<ApplicationUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager()
.AddDefaultTokenProviders();

Any help would be greatly appreciated?

Upvotes: 0

Views: 170

Answers (1)

Goober
Goober

Reputation: 13508

I fixed this problem by deleting the IdentitySchema.Designer.cs file, before running the migration again.

Upvotes: 0

Related Questions