Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone,
I am using Entity framework Code first Approach in asp.net C# and I have crated these " Entity classes One Context class and One. Initializer class here.
I want create Database but I am not able to create Database please and tell me what is wrong and why and can not create Database.

public class Product
   {
       [ScaffoldColumn(false)]
       public int ProductID { get; set; }
       [Required, StringLength(100), Display(Name = "Name")]
       public string ProductName { get; set; }
       [Required, StringLength(10000), Display(Name = "Product Description"),
      DataType(DataType.MultilineText)]
       public string Description { get; set; }
       public string ImagePath { get; set; }
       [Display(Name = "Price")]
       public double? UnitPrice { get; set; }
       public int? CategoryID { get; set; }
       public virtual Category Category { get; set; }
   }


public class Category
   {
       [ScaffoldColumn(false)]
       public int CategoryID { get; set; }
       [Required, StringLength(100), Display(Name = "Name")]
       public string CategoryName { get; set; }
       [Display(Name = "Product Description")]
       public string Description { get; set; }
       public virtual ICollection<Product> Products { get; set; }
   }


public class ProductContext:DbContext
   {
       public ProductContext(): base("protest")
       {

       }
       public DbSet<Category> Categories { get; set; }
       public DbSet<Product> Products { get; set; }
   }


public class ProductDatabaseInitializer : DropCreateDatabaseAlways<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()
        {
            var categories = new List<Category> {
            new Category
             {
                 CategoryID = 1,
                 CategoryName = "Cars"
             },
             new Category
             {
                 CategoryID = 2,
                 CategoryName = "Planes"
             },
             new Category
             {
                 CategoryID = 3,
                 CategoryName = "Trucks"
             },
             new Category
             {
                 CategoryID = 4,
                 CategoryName = "Boats"
             },
             new Category
             {
                 CategoryID = 5,
                 CategoryName = "Rockets"
             },
             };
            return categories;
        }

        // NEW Method start here by Tahir 

        private static List<Product> GetProducts()
        {
            var products = new List<Product> {
             new Product
             {
                  ProductID = 1,
                  ProductName = "Convertible Car",
                  Description = "This convertible car is fast! The engine is powered by a neutrino based battery (not included)." + "Power it up and let it go!",
                  ImagePath="carconvert.png",
                  UnitPrice = 22.50,
                  CategoryID = 1
             },


             new Product
             {
                 ProductID = 2,
                 ProductName = "Old-time Car",
                 Description = "There's nothing old about this toy car,except it's looks. Compatible with other old toy cars.",
                 ImagePath="carearly.png",
                 UnitPrice = 15.95,
                 CategoryID = 1

             },
             new Product
             {
                ProductID = 3,
                ProductName = "Fast Car",
                Description = "Yes this car is fast, but it also floats in water.",
                ImagePath="carfast.png",
                UnitPrice = 32.99,
                CategoryID = 1



             },
             new Product
             {
              ProductID = 4,
              ProductName = "Super Fast Car",
              Description = "Use this super fast car to entertain guests.Lights and doors work!",
              ImagePath="carfaster.png",
              UnitPrice = 8.95,
              CategoryID = 1


             },
             new Product
            {
              ProductID = 5,
              ProductName = "Old Style Racer",
              Description = "This old style racer can fly (with user assistance). Gravity controls flight duration." + "No batteries required.",
              ImagePath = "carracer.png",
              UnitPrice = 34.95,
              CategoryID = 1
            },




             new Product
            {
              ProductID = 6,
              ProductName = "Ace Plane",
              Description = "Authentic airplane toy. Features realistic color and details.",
              ImagePath="planeace.png",
              UnitPrice = 95.00,
              CategoryID = 2


             },
             new Product
             {
              ProductID = 7,
              ProductName = "Glider",
              Description = "This fun glider is made from real balsa wood.Some assembly required.",
              ImagePath="planeglider.png",
              UnitPrice = 4.95,
              CategoryID = 2


             },
             new Product
             {
              ProductID = 8,
              ProductName = "Paper Plane",
              Description = "This paper plane is like no other paper plane.Some folding required.",
              ImagePath = "planepaper.png",
              UnitPrice = 2.95,
              CategoryID = 2


             },
             new Product
             {
              ProductID = 9,
              ProductName = "Propeller Plane",
              Description = "Rubber band powered plane features two  wheels.",
              ImagePath = "planeprop.png",
              UnitPrice = 32.95,
              CategoryID = 2


             },
             new Product
             {
              ProductID = 10,
              ProductName = "Early Truck",
              Description = "This toy truck has a real gas powered engine.Requires regular tune ups.",
              ImagePath = "truckearly.png",
              UnitPrice = 15.00,
              CategoryID = 3


             },
             new Product
             {
              ProductID = 11,
              ProductName = "Fire Truck",
              Description = "You will have endless fun with this one  quarter sized fire truck.",

              ImagePath="truckfire.png",
              UnitPrice = 26.00,
              CategoryID = 3
             },
             new Product
             {
              ProductID = 12,
              ProductName = "Big Truck",
              Description = "This fun toy truck can be used to tow other trucks that are not as big.",
              ImagePath="truckbig.png",
              UnitPrice = 29.00,
              CategoryID = 3


             },
             new Product
             {
              ProductID = 13,
              ProductName = "Big Ship",
              Description = "Is it a boat or a ship. Let this floating vehicle decide by using its " +  "artifically intelligent computer brain!",
              ImagePath = "boatbig.png",
              UnitPrice = 95.00,
              CategoryID = 4



             },
             new Product
             {
              ProductID = 14,
              ProductName = "Paper Boat",
              Description = "Floating fun for all! This toy boat can be assembled in seconds.Floats for minutes!" + "Some folding required.",
              ImagePath="boatpaper.png",
              UnitPrice = 4.95,
              CategoryID = 4



             },
             new Product
             {
              ProductID = 15,
              ProductName = "Sail Boat",
              Description = "Put this fun toy sail boat in the water and let it go!",
              ImagePath="boatsail.png",
              UnitPrice = 42.95,
              CategoryID = 4


             },
             new Product
             {
              ProductID = 16,
              ProductName = "Rocket",
              Description = "This fun rocket will travel up to a height  of 200 feet.",
              ImagePath = "rocket.png",
               UnitPrice = 122.95,
               CategoryID = 5




             }
         };
            return products;



        }

    }


and this code is written in Globel.asax there is any error but i am not able to create Dabsebase please and tell me what is wrong and why and can not create Database

protected void Application_Start(object sender, EventArgs e)
       {

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


       }


What I have tried:

Hi everyone,
I have tried many times to solve this issue but I am not able to resolve this issue.
I am using Entity framework Code first Approach in asp.net C# and I have crated these " Entity classes One Context class and One. Initializer class here.
I want create Database but I am not able to create Database please and tell me what is
wrong and why and can not create Database.
Posted
Comments
Dave Kreskowiak 2-Apr-22 16:23pm    
You never said what database engine you're using, nor what the connection string looks like, nor the error messages you're getting.
Member 10267889 3-Apr-22 15:30pm    
HI, here is my Connection string.


<connectionstrings>

<add name="protest" connectionstring="Data Source=LOCALHOST\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\protest.mdf;Integrated Security=True" providername="System.Data.SqlClient">
Dave Kreskowiak 3-Apr-22 17:27pm    
...and the error messages?
Member 10267889 7-Apr-22 4:59am    
There is no any kind of error. when first time go cursor Globel.asax file read it and later disappear even i do not no know where my cursor go. there is no error
Dave Kreskowiak 7-Apr-22 9:07am    
So how do you know it's not creating the database?

Any errors are going to show up in the Visual Studio Output window when you run the app under the debugger.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900