I learnt that we need to add a new Database Migration to an asp project using the command.
dotnet ef migrations add ClientMigrations
The command works and a class file is generated in the Migrations folder.
However when I try to run the command
UPDATE DATABASE
in the PM CONSOLE then the Table creation command fails with this message
Microsoft.EntityFrameworkCore.Migrations[20402]
Applying migration '20230623163254_InitialCreate'.
Applying migration '20230623163254_InitialCreate'.
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE "ClientMessages" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_ClientMessages" PRIMARY KEY AUTOINCREMENT,
"Name" TEXT NOT NULL,
"Message" TEXT NOT NULL,
"Time" TEXT NOT NULL
);
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20230623163254_InitialCreate', '7.0.7');
Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20230623163254_InitialCreate', '7.0.7');
Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such table: __EFMigrationsHistory'.
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.PrepareAndEnumerateStatements(Stopwatch timer)+MoveNext()
at Microsoft.Data.Sqlite.SqliteCommand.GetStatements(Stopwatch timer)+MoveNext()
at Microsoft.Data.Sqlite.SqliteDataReader.NextResult()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
SQLite Error 1: 'no such table: __EFMigrationsHistory'.
Check the section below for the implementation details of my database context class and model.
What I have tried:
Dbctx.cs
using Microsoft.EntityFrameworkCore;
namespace ASPLearner.Models
{
public class DbCtx : DbContext
{
public DbSet<ClientMessage> ClientMessages { get; set; }
public DbCtx(DbContextOptions<DbCtx> options) : base(options) {
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ClientMessage>().ToTable("ClientMessages");
}
}
}
CientMessage.cs
namespace ASPLearner.Models
{
public class ClientMessage
{
public int Id { get; set; }
public string Name { get; set; }
public string Message { get; set; }
public DateTime Time { get; set; }
}
}
Code generated in the migration file
<pre>using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ASPLearner.Migrations
{
public partial class ClientMigrations : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ClientMessages",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
Message = table.Column<string>(type: "TEXT", nullable: false),
Time = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ClientMessages", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ClientMessages");
}
}
}
Help me get rid of that error so that my program creates the table in the database, Thanks