简体   繁体   中英

my database won't seed using entity framework code first

I have been trying with mixed success to build a website for the past few weeks and suddenly today my database won't seed.

I did add another class and add some more data to my DatabaseInitialiser.cs file so I'm assuming from what I've read that this has caused some issue.

Everything seems to run fine however the database tables are empty.

I'm running Visual Studio Express 2013 for Web.

OK here's the code, let me know if I've forgotten anything. Any help is muchly appreciated :-)

Global.asax.cs:

// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

// Initialize the product database.
Database.SetInitializer(new DatabaseInitialiser());

DatabaseInitialiser.cs:

public class DatabaseInitialiser : DropCreateDatabaseAlways<YPGOOSDataContext>
{
    protected override void Seed(YPGOOSDataContext context)
    {
        GetCategories().ForEach(c => context.Categories.Add(c));
        GetProducts().ForEach(p => context.Products.Add(p));
        GetSizes().ForEach(s => context.Sizes.Add(s));
        GetQuantityBreaks().ForEach(q => context.QuantityBreaks.Add(q));
        GetStyles().ForEach(st => context.Styles.Add(st));
        GetStocks().ForEach(sto => context.Stocks.Add(sto));
    }

    private static List<Category> GetCategories()
    {
        var categories = new List<Category> {
            new Category
            {
                CategoryID = 1,
                ...etc

YPGOOSDataContext.cs:

namespace YPGOOS2.Models
{
    public class YPGOOSDataContext : DbContext
    {
        public YPGOOSDataContext() : base("YPGOOS2") {}

        public DbSet<Category> Categories { get; set; }
        public DbSet<Style> Styles { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Size> Sizes { get; set; }
        public DbSet<QuantityBreak> QuantityBreaks { get; set; }
        public DbSet<Stock> Stocks { get; set; }
    }
}

Call context.SaveChanges() after you're done...

protected override void Seed(YPGOOSDataContext context)
{
    GetCategories().ForEach(c => context.Categories.Add(c));
    GetProducts().ForEach(p => context.Products.Add(p));
    GetSizes().ForEach(s => context.Sizes.Add(s));
    GetQuantityBreaks().ForEach(q => context.QuantityBreaks.Add(q));
    GetStyles().ForEach(st => context.Styles.Add(st));
    GetStocks().ForEach(sto => context.Stocks.Add(sto));

    context.SaveChanges();
}

OK I finally figured out the answer after 2.5 days of searching and debugging. There was two things wrong; 1. when seeding the data like this var stocks = new List { new Stock { StockID=1, etc I had the var stocks as var Stocks which I believe was causing some issues. 2. I had a , after the final } when adding the last stock. This didn't throw any errors it behaved as normal but just didn't seed the database!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM