I added a new table to my existing SQL Server database. I am using its generated id as the key. I ran dotnet ef migration add NewTable, which ran without errors. I reviewed the migration file and this is what was added for my new table:
migrationBuilder.CreateTable( name: "InboxNotifications", columns: table => new { Id = table.Column<string>(type: "nvarchar(450)", nullable: false), Created = table.Column<DateTime>(type: "datetime2", nullable: false), CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true), DataId = table.Column<string>(type: "nvarchar(max)", nullable: true), EventIdent = table.Column<string>(type: "nvarchar(max)", nullable: true), Message = table.Column<string>(type: "nvarchar(max)", nullable: true), Status = table.Column<int>(type: "int", nullable: false), TeamIdent = table.Column<string>(type: "nvarchar(max)", nullable: true), Title = table.Column<string>(type: "nvarchar(max)", nullable: true), Type = table.Column<int>(type: "int", nullable: false), UserId = table.Column<string>(type: "nvarchar(max)", nullable: true) }, constraints: table => { table.PrimaryKey("PK_InboxNotifications", x => x.Id); }); Now, when I run dotnet ef database update I get this error:
fail: Microsoft.EntityFrameworkCore.Database.Command[200102]
Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [ApplicationUserToken] DROP CONSTRAINT [FK_ApplicationUserToken_AspNetUsers_UserId];
System.Data.SqlClient.SqlException (0x80131904): Cannot find the object "ApplicationUserToken" because it does not exist or you do not have permissions.at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)2 parameterValues)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary
I'm not quite sure what that means as I did not change anything else within my db context file. I only added the new model.
EDIT:
So I have this function called in Configure() inside startup.cs:
`
public virtual void EnsureDatabaseCreated(TennisFolderContext dbContext, UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager, bool createData) { // run Migrations DbInitializer.Initialize(dbContext, userManager, roleManager, createData).Wait(); } `
According to https://learn.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/
Moving it to program.cs did not fix it. Can't seem to figure out what is going on.