question

kobosh-5924 avatar image
0 Votes"
kobosh-5924 asked YijingSun-MSFT answered

EF 6 seed not working

I am following through with EF6 tutorial


I followed the steps copied code from website into visual studio 2013. First problem the database was not created; I found solution by adding this to Global Application_Start method

  ProductContext context = new ProductContext();
             context.Database.Initialize(true);

Database was created but without seeding.
Please help

dotnet-aspnet-mvcdotnet-entity-framework
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@kobosh-5924, what is your current app, asp.net or winform? >>Database was created but without seeding. Do you want to insert data to database but failed? If so, Please provide the related code about it.

0 Votes 0 ·

Thanks Jack. the link to walkthrough I am following

create-the-project


I I created repo with simplified version of tutorial


0 Votes 0 ·
Show more comments

Thanks Jack. The Seed method is not being called ever. I get empty database.

namespace WingtipToys.Models
{
public class ProductContext : DbContext
{
public ProductContext() : base("WingtipToys")
{
}
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
}
}


namespace WingtipToys.Models
{
public class ProductDatabaseInitializer : DropCreateDatabaseIfModelChanges<ProductContext>
{
protected override void Seed(ProductContext context)
{
GetCategories().ForEach(c => context.Categories.Add(c));
GetProducts().ForEach(p => context.Products.Add(p));
}

 private static List<Category> GetCategories()
 {
      

   return categories;
 }

 private static List<Product> GetProducts()
 {
      
   return products;
 }

}
}

void Application_Start(object sender, EventArgs e)

{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//CWingtipToysLib.Models.Main.GetCategs();
ProductContext contxt=new ProductContext();
// contxt.Database.Initialize(false);
Database.SetInitializer(new ProductDatabaseInitializer());

}






0 Votes 0 ·

1 Answer

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @kobosh-5924 ,
I think there are two ways to call the seed() method.First,you could call Update-Database from the Package Manager Console.
You could refer to below article:
https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-3
Second,you could call the seed method by yourself. You could change your InitializeDatabase method. Just like this:
DatabaseContext class:

 public DatabaseContext() : base("DatabaseContext")
  {
     InitializeDatabase();
  }
    
  public DatabaseContext(string connectionString) : base(connectionString)
  {
      Database.Connection.ConnectionString = connectionString;
      InitializeDatabase();
  }
    
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  }

changed my InitializeDatabase method from:

 private void InitializeDatabase()
 {
     Database.SetInitializer(new DatabaseInitializer());
     if (!Database.Exists())
     {
         Database.Initialize(true);
     }            
 }

to:

 protected virtual void InitializeDatabase()
 {
     if (!Database.Exists())
     {
         Database.Initialize(true);
         new DatabaseInitializer().Seed(this);
     }            
 }

More details,you could refer to below article:
https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.database.initialize?redirectedfrom=MSDN&view=entity-framework-6.2.0#overloads

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.