Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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# ASP。NET Core 2.2 EF-数据库未种子设定_C#_Entity Framework_Web Applications_Asp.net Core 2.2 - Fatal编程技术网

C# ASP。NET Core 2.2 EF-数据库未种子设定

C# ASP。NET Core 2.2 EF-数据库未种子设定,c#,entity-framework,web-applications,asp.net-core-2.2,C#,Entity Framework,Web Applications,Asp.net Core 2.2,我正在使用Asp.NETCore2.2和EntityFramework开发一个MVCWeb应用程序。我对这个框架非常陌生,我正在尝试为数据库添加种子,但是在测试中,数据库仍然是空的。我看过以前的文章,关于通过Program.cs调用我的Initialize方法,我试图实现它,所以我不确定我错过了什么? 我试图找到一个可能发生的问题的答案,但其他解决方案指向该框架的早期版本 任何帮助都将不胜感激 我的数据库初始化方法如下: public static void Initialize(Applica

我正在使用Asp.NETCore2.2和EntityFramework开发一个MVCWeb应用程序。我对这个框架非常陌生,我正在尝试为数据库添加种子,但是在测试中,数据库仍然是空的。我看过以前的文章,关于通过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();
        }
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; }
}
我的Program.cs课程如下:

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

}

您的项目由于默认打开的IDENTITY\u INSERT而失败。实体框架按约定将Id/Id视为主键,无法插入具有给定Id的记录。如果您不想将此字段视为主键,请重命名此字段,但我建议保持原样并允许其工作


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

你好,琼斯。请加上附带类型的型号好吗?是Id[键]?在这种情况下,由于启用了IDENTITY\u insert,因此无法插入具有给定Id的值。此外,如果这是正常的整数字段,请检查表IncidentType是否由于first if condition.Hi而被设定为种子。我添加了IncidentType模型。我已尝试删除要检查的第一个If语句,但没有成功。按照约定,名为Id或Id的属性将配置为实体的密钥。重命名Id字段或从播种机中删除Id初始化。这应该会有帮助。我从播种机上删除了Id,它现在可以工作了!谢谢你的帮助