ASP。 NET Core 2.2 EF - 数据库未播种

ASP. NET Core 2.2 EF - Database Not Seeding

我正在使用 Asp .Net Core 2.2 和 EntityFramework 开发 MVC Web 应用程序。我对这个框架很陌生,我正在尝试为数据库播种,但在测试时,数据库仍然是空的。我看过以前关于通过 Program.cs 调用我的 Initialize 方法的帖子,我试图实现它,所以我不确定我错过了什么? 我试图找到可能发生的问题的答案,但其他解决方案指向该框架的早期版本。

如有任何帮助,我们将不胜感激!

我的数据库初始化方法如下:

public static void Initialize(ApplicationDbContext context)
    {
        {
            //Look for any IncidentTypes
            if (context.IncidentType.Any())
            {
                return; // DB has been seeded
            }

            var incidentTypes = new IncidentType[]
            {
                 new IncidentType { Id = 1, Type = "Urgent"},
                 new IncidentType { Id = 2,Type = "Non-Urgent"}
            };

            foreach (IncidentType t in incidentTypes)
            {
                context.IncidentType.Add(t);
            }
            context.SaveChanges();

            var outcomes = new Outcome[]
            {
                new Outcome { Id = 1, OutcomeText = "This type of incident does not apply"},
                new Outcome { Id = 2, OutcomeText = "Go to ED"},
                new Outcome { Id = 3, OutcomeText = "Go to ED"},
                new Outcome { Id = 4, OutcomeText = "CLEAR & LEAVE"}
            };

            foreach (Outcome o in outcomes)
            {
                context.Outcome.Add(o);
            }
            context.SaveChanges();

            var stages = new Stage[]
            {
                new Stage { Id = 1, OutcomeId = 1, StageName = "Complete PS"},
                new Stage { Id = 2, OutcomeId = 2, StageName = "Do Any Apply?"},
                new Stage { Id = 3, OutcomeId = 3, StageName = "Do Any Apply?"},
                new Stage { Id = 4, OutcomeId = 4, StageName = "Do Any Apply?"}
            };

            foreach (Stage s in stages)
            {
                context.Stage.Add(s);
            }
            context.SaveChanges();

            var stageConditions = new StageCondition[]
            {
                new StageCondition { Id = 1, IncidentTypeId = 1, StageId = 1,
                    ConditionText = "This process does not apply to the following: INSERT"},
                new StageCondition { Id = 2, IncidentTypeId = 1, StageId = 2,
                    ConditionText = "First set of Questions"},
                new StageCondition { Id = 3, IncidentTypeId = 1, StageId = 3,
                    ConditionText = "Second set of Questions"},
                new StageCondition { Id = 4, IncidentTypeId = 1, StageId = 4,
                    ConditionText = "Is there a suitable ED alternative?"},
                new StageCondition { Id = 5, IncidentTypeId = 2, StageId = 1,
                    ConditionText = "This process does not apply to the following: INSERT"},
                new StageCondition { Id = 6, IncidentTypeId = 2, StageId = 2,
                    ConditionText = "First set of Questions"},
                new StageCondition { Id = 7, IncidentTypeId = 2, StageId = 3,
                    ConditionText = "Second set of Questions"},
                new StageCondition { Id = 8, IncidentTypeId = 2, StageId = 4,
                    ConditionText = "Is there a suitable ED alternative?"}
            };

            foreach (StageCondition c in stageConditions)
            {
                context.StageCondition.Add(c);
            }
            context.SaveChanges();
        }

我的Program.csclass如下:

public class Program
{
    public static void Main(string[] args)
    {
        var host = CreateWebHostBuilder(args).Build();

        using (var scope = host.Services.CreateScope())
        {
            var services = scope.ServiceProvider;

            try
            {
                var context = services.GetRequiredService<ApplicationDbContext>();
                DbInitializer.Initialize(context);
            }
            catch (Exception ex)
            {
                var logger = services.GetRequiredService<ILogger<Program>>();
                logger.LogError(ex, "An error occured creating the DB.");
            }
        }

        host.Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

我的数据库上下文如下:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    public DbSet<Models.IncidentType> IncidentType { get; set; }
    public DbSet<Models.Incident> Incident { get; set; }
    public DbSet<Models.StageCondition> StageCondition { get; set; }
    public DbSet<Models.Outcome> Outcome { get; set; }
    public DbSet<Models.Stage> Stage { get; set; }
}

编辑: 在下面添加了事件类型模型:

    public class IncidentType
{
    public int Id { get; set; }
    public string Type { get; set; }
}

}

您的项目因 IDENTITY_INSERT 而失败,默认情况下它是打开的。 Entity framework 按照惯例将 Id /Id 视为主键,并且无法插入具有给定 "Id" 的记录。如果您不想将其视为主键,请重命名该字段,但我建议您保持原样并允许它工作。

此外,我建议阅读有关依赖注入和将 DatabaseSeeder 作为单例注入以在应用程序启动后创建一个实例的内容。在程序 class 中插入任何类型的逻辑都是不好的做法。