我有一个用string UserID
和int EventID
定义的UserCalendar模型。我的ApplicationDbContext类包含一个名为DbSet
的{{1}} UserCalendars声明。不知何故,Calendars表的create-table语句如下:
Calendars
我认为这些多余的栏目阻止我在表格中添加任何记录,因为绝大多数都没有意义;日历是用户和事件之间的链接,因此只需要外键。
是什么导致create-table语句包含多余的列,以及如何解析?
CREATE TABLE [dbo].[Calendars] (
[Id] NVARCHAR (450) NOT NULL,
[AccessFailedCount] INT NOT NULL,
[ConcurrencyStamp] NVARCHAR (MAX) NULL,
[Email] NVARCHAR (MAX) NULL,
[EmailConfirmed] BIT NOT NULL,
[EventID] INT NOT NULL,
[LockoutEnabled] BIT NOT NULL,
[LockoutEnd] DATETIMEOFFSET (7) NULL,
[NormalizedEmail] NVARCHAR (MAX) NULL,
[NormalizedUserName] NVARCHAR (MAX) NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[PhoneNumber] NVARCHAR (MAX) NULL,
[PhoneNumberConfirmed] BIT NOT NULL,
[SecurityStamp] NVARCHAR (MAX) NULL,
[TwoFactorEnabled] BIT NOT NULL,
[UserID] NVARCHAR (MAX) NOT NULL,
[UserName] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_Calendars] PRIMARY KEY CLUSTERED ([Id] ASC)
);
UPDATE:UserCalendar类最初继承自IdentityUser。这已经修复了。尝试使用全新迁移运行Update-Database时,现在会创建以下错误消息:
public class UserCalendar
{
[Required]
public int UserCalendarID { get; set; }
[Required]
public string UserID { get; set; }
[Required]
public int EventID { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
public DbSet<Genre> Genres { get; set; }
public DbSet<Event> Events { get; set; }
public DbSet<ApplicationUser> Artists { get; set; }
public DbSet<UserCalendar> Calendars { get; set; }
}
使用以下迁移尝试删除表,但这会产生相同的错误:
System.Data.SqlClient.SqlException: The object 'PK_Calendars' is dependent on column 'ID'.
ALTER TABLE ALTER COLUMN ID failed because one or more objects access this column.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
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(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.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 contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:90d49e6f-8365-4355-9470-fcf2745bb91b
Error Number:5074,State:1,Class:16
The object 'PK_Calendars' is dependent on column 'ID'.
ALTER TABLE ALTER COLUMN ID failed because one or more objects access this column.