我使用Entity Framework Core3。我创建了新的Present
文件
Present.cs
public class Present
{
[Key]
public string PresentedId { get; set; }
public string ReagentId { get; set; }
[Column(TypeName ="smalldatetime")]
public DateTime Date { get; set; }
public bool Paid { get; set; }
public double ShareAmount { get; set; }
[ForeignKey("ReagentId")]
public virtual AppUser Reagent { get; set; }
[ForeignKey("PresentedId")]
public virtual AppUser Presented { get; set; }
}
我的DBContext
public class AppDbContext : IdentityDbContext<AppUser, IdentityRole, string>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
///...
public virtual DbSet<Present> Presents { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
///...
builder.Entity<Present>()
.Property(e => e.Date)
.HasDefaultValueSql("getutcdate()");
builder.Entity<AppUser>()
.HasOne(e => e.Reagent)
.WithOne(e => e.Presented);
builder.Entity<AppUser>()
.HasMany(e => e.Presents)
.WithOne(e => e.Reagent)
.HasForeignKey(e => e.ReagentId)
.OnDelete(DeleteBehavior.NoAction);
}
}
我添加了迁移
dotnet ef migrations add addedPresent ///this was successfull,and was not empty for first time
dotnet ef database update /// this was with error
但是当我想更新数据库时,我遇到了一个错误,应该删除Cascad。在数据库中,它没有创建新表(Present
),也没有在Migrations table
中添加新行,因此,我删除了上一次迁移我将OnDelete(DeleteBehavior.Cascade);
更改为OnDelete(DeleteBehavior.NoAction);
然后我添加了新的Migrations。但是我的问题是它将创建一个空的Migrations文件,我不知道如何解决这个问题?
public partial class addpresent : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
修改
我使用以下命令删除迁移
dotnet ef migrations remove --force
并执行错误,但删除了迁移文件(addPresent.cs)
dotnet ef迁移删除--force 信息:Microsoft.EntityFrameworkCore.Infrastructure [10403] 实体框架核心3.0.0使用提供程序'Microsoft.EntityFrameworkCore.SqlServer'初始化了'AppDbContext',选项如下: 执行DbCommand [Parameters = [],CommandType ='Text',CommandTimeout = '30'] SELECT OBJECT_ID(N'[__ EFMigrationsHistory]'); 信息:Microsoft.EntityFrameworkCore.Database.Command [20100] 执行DbCommand [Parameters = [],CommandType ='Text',CommandTimeout = '30'] SELECT OBJECT_ID(N'[__ EFMigrationsHistory]'); 执行DbCommand [Parameters = [],CommandType ='Text',CommandTimeout = '30'] 选择[MigrationId],[ProductVersion] 来自[__EFMigrationsHistory] ORDER BY [MigrationId]; 信息:Microsoft.EntityFrameworkCore.Database.Command [20100] 执行DbCommand [Parameters = [],CommandType ='Text',CommandTimeout = '30'] 选择[MigrationId],[ProductVersion] 来自[__EFMigrationsHistory] ORDER BY [MigrationId]; 正在删除迁移'20191124071753_addPresent'。 System.NullReferenceException:对象引用未设置为对象的实例。 在Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(字符串值) 在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GeneratePropertyAnnotations(IProperty属性,IndentedStringBuilder stringBuilder)中 在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperty(String builderName,IProperty属性,IndentedStringBuilder stringBuilder) 在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperties(String builderName,IEnumerable
1 properties, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder) at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList
1 entityTypes,IndentedStringBuilder stringBuilder)中 在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(字符串builderName,IModel模型,IndentedStringBuilder stringBuilder) 在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateSnapshot(String modelSnapshotNamespace,Type contextType,String modelSnapshotName,IModel模型) 在Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir,String rootNamespace,Boolean force,String语言) 在Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.RemoveMigration(String contextType,Boolean force) 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigrationImpl(String contextType,Boolean force)处 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigration。<> c__DisplayClass0_0。<。ctor> b__0() 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase。<> c__DisplayClass3_0`1.b__0() 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action操作) 对象引用未设置为对象的实例。
我添加了“新迁移”,但它仍为空文件
dotnet ef migrations add addPresent
编辑2:
现在,我添加了一个表Conversation
(用于测试),并且添加了迁移功能,并且可以正常运行
dotnet ef database update
它向数据库添加了表。但是数据库中没有Present
表。我不知道它如何检测到我添加了Conversation
却没有检测到我添加了Present
。现在该怎么办?