实体框架核心 - 数据库未填充

时间:2018-02-25 19:56:00

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

我试图用StoreInitializer填充数据库,但表是空的,我使用的是Entity Framework Core和ASP.NET Core,我想配置我的初始化程序以使用更新 - 包管理器控制台中的数据库。我遵循了本教程https://docs.microsoft.com/pl-pl/aspnet/core/data/ef-mvc/intro,但在开始或更新时,无论如何数据库都是空的。 Visual studio告诉我没有错误。

StoreInitializer.cs:

namespace PC_SHOP.DAL
{
    public static class StoreInitializer
    {


        public static void Initialize(StoreContext context)
        {

            context.Database.EnsureCreated();

            if (context.Electronics.Any())
            {
                return;
            }


            // Działy główne
            var electronics = new Electronic[]
            {
                new Electronic { ID=1, Name = "Laptopy"},
                new Electronic { ID=2, Name = "Komputery"},
                new Electronic { ID=3, Name = "Części PC"},
                new Electronic { ID=4, Name = "Dla graczy"},
                new Electronic { ID=5, Name = "Peryferia PC"},
                new Electronic { ID=6, Name = "Sieci i komunikacja"},
                new Electronic { ID=7, Name = "Oprogramowanie"},

            };

            foreach (Electronic e in electronics)
            {
                context.Electronics.Add(e);
            }

            context.SaveChanges();



            // Rodzaje laptopów
            var laptops = new List<Laptop>
            {
                new Laptop { ID=1, Name = "Laptopy", ElectronicID = 1, IconFileName = "1_laptop.jpg"},
                new Laptop { ID=2, Name = "Laptopy Apple", ElectronicID = 1, IconFileName = "2_apple.jpg"},

            };

            foreach (Laptop l in laptops)
            {
                context.Laptops.Add(l);
            }

            context.SaveChanges();


        }


    }
}

StoreContext.cs:

namespace PC_SHOP.DAL
{
    public class StoreContext : DbContext
    {
        public StoreContext(DbContextOptions<StoreContext> options) : base(options)
        {

        }

        public DbSet<Electronic> Electronics { get; set; }
        public DbSet<Laptop> Laptops { get; set; }
        public DbSet<LaptopProduct> LaptopProducts { get; set; }
        public DbSet<Employee> Employee { get; set; }
        public DbSet<Project> Project { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Electronic>().ToTable("Electronic");
            modelBuilder.Entity<Laptop>().ToTable("Laptop");
            modelBuilder.Entity<LaptopProduct>().ToTable("LaptopProduct");
            modelBuilder.Entity<Employee>().ToTable("Employee");
            modelBuilder.Entity<Project>().ToTable("Project");
        }



    }
}

在Startup.cs的ConfigurationServies中,我添加了:

services.AddDbContext<StoreContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

并在appsettings.json中:

"ConnectionStrings": {
    "DefaultConnection": "Data Source=DESKTOP-J0OBBIO\\SQLEXPRESS;Initial Catalog=PC_SHOP;Integrated Security=SSPI;Database=Employee;Trusted_Connection=True;MultipleActiveResultSets=true"
  },  

2 个答案:

答案 0 :(得分:1)

将此行添加到Program.cs Main()方法中: StoreInitializer.Initialize(context);

length

答案 1 :(得分:0)

EnsureCreated可用于在原型设计和测试场景中创建数据库和表。它应该代替,而不是与迁移一起使用。只有在数据库不存在或不包含表时才会创建数据库。在其他情况下,它什么都不做。

请注意,如果数据库存在,它将不会执行任何操作,因此您需要使用不存在的数据库名称。

实际上,您应该从命令行生成迁移,然后从初始化程序运行迁移,而不是使用.EnsureCreated。 EnsureCreated用于每次创建的内存数据库。即原型制作。