种子方法不填充表

时间:2018-10-02 15:07:01

标签: entity-framework-6 seeding

数据库和表均已成功创建。但是,无论是将种子代码放入配置文件中还是用作初始化程序。这些表永远不会填充数据。

我尝试过的事情: 1.我查看过关于stackoverflow的几篇文章,将我的代码与解决方案进行了比较,无济于事。 2.我将DropCreateDatabaseIfModelChanges更改为DropCreateDatabaseAlways,仍然相同。 3.我已确认所有必填字段均具有值

public class BankAccountInitializer : DropCreateDatabaseAlways<BankAccountContext>
{
    protected override void Seed(BankAccountContext context)
    {

        var bank1 = new Bank(1) { BankName = "Huntington", BankAccounts = new List<BankAccount>() };

        context.Banks.Add(bank1);

        context.BankAccounts.Add
        (
            new CheckingAccount(1)
            {
                BankId = 1,
                AccountBalance = 25.00m,
                BankAccountName = "Checking Account 1",
                DateAdded = DateTime.Now,
                RoutingNumber = 00000887,
                Owner = "Checking Account Owner 1",
                Bank = bank1
            }
        );

        context.BankAccounts.Add
        (
              new IndividualInvestment(2)
              {
                  BankId = 1,
                  AccountBalance = 10.00m,
                  BankAccountName = "Individual Investment 1",
                  DateAdded = DateTime.Now,
                  RoutingNumber = 00000887,
                  Owner = "Individual Investment Owner 1",
                  Bank = bank1
              }
        );

        context.BankAccounts.Add(

            new CorporateInvestment(3)
            {
                BankId = 1,
                AccountBalance = 98.00m,
                BankAccountName = "Corporate Investment 1",
                DateAdded = DateTime.Now,
                RoutingNumber = 00000887,
                Owner = "Corporate Investment Owner 1",
                Bank = bank1
            }
         );

        base.Seed(context);
   }
}

}

0 个答案:

没有答案