Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么迁移添加会创建空迁移文件?_C#_Entity Framework_.net Core_Entity Framework Core_Entity Framework Migrations - Fatal编程技术网

C# 为什么迁移添加会创建空迁移文件?

C# 为什么迁移添加会创建空迁移文件?,c#,entity-framework,.net-core,entity-framework-core,entity-framework-migrations,C#,Entity Framework,.net Core,Entity Framework Core,Entity Framework Migrations,我使用实体框架核心3。我创建了新的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 boo

我使用实体框架核心3。我创建了新的
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);
    }
}
但当我想更新数据库时,出现了一个应该删除Cascad的错误。在数据库中,它没有创建新表(
Present
),也没有在
Migrations table
中添加新行,所以,我删除了上一次迁移,并更改了
OnDelete(DeleteBehavior.Cascade)
to
OnDelete(DeleteBehavior.NoAction)
然后我添加了新的迁移。但我的问题是,它将创建空的迁移文件,我不知道如何解决这个问题

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迁移删除--强制 信息:Microsoft.EntityFrameworkCore.Infrastructure[10403] 实体框架核心3.0.0使用提供程序“Microsoft.EntityFrameworkCore.SqlServer”初始化了“AppDbContext”,选项为:无 正在执行DbCommand[Parameters=[],CommandType='Text',CommandTimeout='30'] 选择对象ID(N'[\u EFMigrationsHistory]'); 信息:Microsoft.EntityFrameworkCore.Database.Command[20100] 正在执行DbCommand[Parameters=[],CommandType='Text',CommandTimeout='30'] 选择对象ID(N'[\u EFMigrationsHistory]'); 正在执行DbCommand[Parameters=[],CommandType='Text',CommandTimeout='30'] 选择[MigrationId],[ProductVersion] 来自[_EFMigrationsHistory] 按[MigrationId]排序; 信息:Microsoft.EntityFrameworkCore.Database.Command[20100] 正在执行DbCommand[Parameters=[],CommandType='Text',CommandTimeout='30'] 选择[MigrationId],[ProductVersion] 来自[_EFMigrationsHistory] 按[MigrationId]排序; 正在删除迁移“20191124071753_addPresent”。 System.NullReferenceException:对象引用未设置为对象的实例。 位于Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(字符串值) 位于Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GeneratePropertyAnnotations(IProperty属性,缩进stringBuilder stringBuilder) 位于Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperty(字符串生成器名称、IProperty属性、缩进字符串生成器stringBuilder stringBuilder) 位于Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperties(字符串生成器名称、IEnumerable
1属性、缩进的stringBuilder stringBuilder)
位于Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName、entityType entityType、IndentedStringBuilder stringBuilder)
在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(字符串生成器名称、IReadOnlyList
1 entityTypes、缩进的stringBuilder stringBuilder) 位于Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(字符串生成器名称、IModel模型、缩进的stringBuilder stringBuilder) 位于Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GeneratesSnapshot(String modelSnapshotNamespace,Type contextType,String modelSnapshotName,IModel model) 位于Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir、String rootNamespace、Boolean force、String language) 位于Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.RemoveMigration(字符串contextType,布尔值强制) 位于Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigrationImpl(字符串上下文类型,布尔强制) 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigration.c__DisplayClass0_0.b_0()中 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.c__DisplayClass3_0`1.b__0()中 位于Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作) 对象引用未设置为对象的实例

我添加了新的迁移,但仍将清空文件

dotnet ef migrations add addPresent
编辑2:

现在我添加了一个表
Conversation
(用于测试),并添加了迁移,它可以正常工作,并且

dotnet ef database update 

它将表添加到数据库中。但数据库中没有
Present
表。我不知道它如何检测到我添加了
Conversation
,但没有检测到我添加了
Present
。现在我该怎么办?

错误发生后,您应该删除最后一次迁移。请参见通过向迁移添加-Force来尝试command@Badri我使用了
--force
,但在添加addPresent之后,它仍然会创建空文件。您必须执行两次remove来删除addPresent和addedPresent迁移。migration是在模型缓存上计算的,缓存当前处于旧的addedPresent迁移,您也必须删除该迁移。删除迁移类是不够的,这将导致此问题
dotnet ef database update