Hi.
I'm working on this project where I'm supposed to make a simple online store using ASP.net, MVC4 and Entity Framework.
I'm on this part where I want to add some products to a database everytime I run my project, and these products are the one I'm going to list on my Homepage (you can buy these products). The problem is that I'm not sure how to add these products to the database everytime I run the project and only then. Earlier before I had experience on loading the data into the database with Page_Load, but since I'm not using Webforms, I cant do it this way.
Is there any ways I can load my product data into database, so I acually can list my products on my website? I want to add the products to the database everytime I start the project, and only then. Want them to stay there as well, do the project wont use so long time to start up everytime.
Thanks in advance.
Here's some of my code:
public class Product
{
[Key]
public int ID { get; set; }
public string ProductName { get; set; }
public double Pris { get; set; }
public string Description { get; set; }
public string GenreName { get; set; }
public virtual Genre Genre { get; set; }
}
public class Genre
{
[Key]
public string GenreName { get; set; }
public List<Product> Products { get; set; }
}
public class ContextClass : DbContext
{
public ContextClass()
: base("name=Product")
{
Database.CreateIfNotExists();
}
public DbSet<Product> Products { get; set; }
public DbSet<KGenre> Genres { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Genre>().HasKey(k => k.GenreName);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
// These are the data I want to add now. How should I do this? What method should I use? Where should I place it?
var gen = new List<Genre>
{
new Genre { GenreName = "Computer" },
new Genre { GenreName = "Cellphones" },
new Genre { GenreName = "Games" }
};
new List<Product>
{
new Produkt { ProductName = "Samsung Galaxy S4", Genre = Genres.Single(k => k.GenreName == "Cellphones"), Pris = 4000.00, Description = "Brand new S4", genreName = "Cellphones" },
new Produkt { ProductName = "LG G2", Genre = Genres.Single(k => k.GenreName == "Cellphones"), Pris = 4500.00, Description = "This is LGG2", genreName = "Cellphones" },
new Produkt { ProductName = "Ipad Mini", Genre = Genres.Single(k => k.GenreName == "Computer"), Pris = 2559.00, Description = "Ipad mini here", genreName = "Computer" },
new Produkt { ProductName = "Nexus 7 (2013)", Genre = Genres.Single(k => k.GenreName == "Computer"), Pris = 2222.00, Description = "This is nexus", genreName = "Computer" },
new Produkt { ProductName = "GTA 5", Genre = Genres.Single(k => k.GenreName == "Games"), Pris = 199.00, Description = "This is GTA", genreName = "Games" },
new Produkt { ProductName = "Fifa 15", Genre = Genres.Single(k => k.GenreName == "Games"), Pris = 40.00, Description = "This is fifa", genreName = "Games" }
}
}
}