Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 3.0 - Can't Update-Database

I just created the project using the command dotnet new angular -o <output_directory_name> -au Individual and scaffold identity then I installed Microsoft.EntityFrameworkCore.SqlServer but when I run the command update-database, I get the error below.

Failed executing DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [AspNetRoles] (
    [Id] TEXT NOT NULL,
    [Name] TEXT(256) NULL,
    [NormalizedName] TEXT(256) NULL,
    [ConcurrencyStamp] TEXT NULL,
    CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
);

and then at the end end another error

Error Number:2716,State:1,Class:16
Column, parameter, or variable #2: Cannot specify a column width on data type text.

Below is the generated CreateIdentitySchema migration

migrationBuilder.CreateTable(
    name: "AspNetRoles",
    columns: table => new
    {
        Id = table.Column<string>(nullable: false),
        Name = table.Column<string>(maxLength: 256, nullable: true),
        NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
        ConcurrencyStamp = table.Column<string>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_AspNetRoles", x => x.Id);
    });

ApplicationDbContextModelSnapshot.cs

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")
        .HasColumnType("nvarchar(256)")
        .HasMaxLength(256);

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

    b.HasKey("Id");

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

    b.ToTable("AspNetRoles");
});

How to fix this errors so I can update-database?

Changing 'TEXT' to 'VARCHAR' in 00000000000000_CreateIdentitySchema.Designer.cs resulted in the following error. The same happens when I change it to 'NVARCHAR'

Data type 'VARCHAR' for property 'Id' is not supported in this form. Either specify the length explicitly in the type name, for example as 'NVARCHAR(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type.
like image 371
Len Avatar asked Oct 14 '19 06:10

Len


People also ask

How do I update Entity Framework model from database first in .NET core?

Updating the Model If you need to re-scaffold the model after database schema changes have been made, you can do so by specifying the -f or --force option e.g.: dotnet ef dbcontext scaffold "Server=. \;Database=AdventureWorksLT2012;Trusted_Connection=True;" Microsoft. EntityFrameworkCore.

How do I update my EDMX in .NET core?

Update the .edmx file when the Database changes In the Model Browser, right-click the . edmx file and select Update Model from Database.


1 Answers

These answers were all helpful, especially the one from @klent. I had generated a new project using:

dotnet new angular -o [MY PROJECT] -au Individual

This produced a new project that used Sqlite as the data provider, although I wasn't aware of that. I changed the connection string to my SQL Server, then ran:

dotnet ef database update

and got the error posted in the original question. After reviewing the answers here I realized that I had the incorrect data provider, and the migrations were targeting Sqlite syntax, which was using TEXT(256) for one of the column specifications. So my specific steps to correct were to:

  1. Remove the Microsoft.EntityFrameworkCore.Sqlite Nuget package.
  2. Install the Microsoft.EntityFrameworkCore.SqlServer Nuget package.
  3. In the ConfigureServices method of my Startup.cs, change the services.AddDbContext call to use options.UseSqlServer instead of options.UseSqlite.
  4. Remove the _CreateIdentitySchema and ApplicationDbContentsModelSnapshot classes from the Migrations folder.
  5. After ensuring the connection string in AppSettings.json points to the SQL Server and not Sqlite, run dotnet ef migrations add CreateIdentitySchema -o Data\Migrations. Note that the output folder specified in the -o parameter may be different for you.
  6. Check the Visual Studio Project Explorer. Two new classes should now appear in the Migrations folder.
  7. Run dotnet ef database update. This will create the tables in the database noted in the connection string.

Additionally, if you want your tables to be in a specific schema, for example "Auth", open your ApplicationDbContext class and add:

protected override void OnModelCreating( ModelBuilder builder )
{
    base.OnModelCreating( builder );
    builder.HasDefaultSchema( "Auth" );
}

However, this will set the schema for all access that uses this DbContext. It wasn't a problem for me since I'm not using EF for my application data, just Identity data. YMMV.

like image 145
Bob Mc Avatar answered Sep 28 '22 03:09

Bob Mc