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

时间:2019-01-25 09:15:12

标签: c# entity-framework web-applications asp.net-core-2.2

我正在使用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.cs类如下:

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; }
}

编辑: 在下面添加了IncidentType模型:

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

}

1 个答案:

答案 0 :(得分:1)

您的项目由于IDENTITY_INSERT而失败,该IDENTITY_INSERT默认情况下为ON。按照惯例,实体框架将Id / Id视为主键,并且无法插入具有给定“ Id”的记录。如果您不想将其视为主键,请重命名此字段,但我建议保留原样并允许其工作。

此外,我建议阅读有关依赖关系注入的信息,并在应用程序启动后以Singleton形式注入DatabaseSeeder以创建一个实例。在Program类中插入任何种类的逻辑都是不好的做法。