none
WingTipToys ASP.NET Tutorial error on files RRS feed

  • Question

  • Im on VS2015 and started the project on .NET 4.5.2 as the example was using that I did not add any of my customs codes but I get these following errors 

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

    ERROR 01 - Red line on base the error (in the above code)
    'object' does not contain a constructor that takes 1 arguments WingtipToys
    I understand that base"WingtipToys" line thats mentions the connection string 
    what I do not understand is that why I am getting errors and other did not 

        using System.Collections.Generic;
        using System.Data.Entity;
        
        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(){...}
                private static List<Category> GetProducts(){...}
            }
        }

    ERROR 2 - Red line below ProductDatabaseInitializer this one I have no idea why :(

    I am a php dev trying ASP.net please help me out 
    Tuesday, June 14, 2016 9:14 AM

Answers

  • Hi faraqsa,

    >>ERROR 01 - Red line on base the error (in the above code)

    According to your code snippet, it seems that we need to inherit DbContext class, like this:

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

    >>ERROR 2 - Red line below ProductDatabaseInitializer this one I have no idea why :(

    According to your code snippet, it's a custom database initialization, You can insert data into your database tables during the database initialization process. This will be important if you want to provide some test data for your application or to provide some default master data for your application. it seems ok. Do you encounter any issue? does it throw any exception?

    For more information about database initialization, please refer to:

    http://www.entityframeworktutorial.net/code-first/seed-database-in-code-first.aspx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 15, 2016 6:52 AM
    Moderator

All replies

  • ERROR 01 - Red line on base the error (in the above code)
    'object' does not contain a constructor that takes 1 arguments
    WingtipToys

    Assuming you know what a class constructor is, it is telling you that there is no parameter that can be passed into the class/object constructor, because the constructor has no code in it to accept a parameter in the constructor.

    My advise to you is stop and take the Database First approach in using EF, which is a lot more simpler to use.

    https://msdn.microsoft.com/en-us/data/jj206878.aspx

    Tuesday, June 14, 2016 2:43 PM
  • Hi faraqsa,

    >>ERROR 01 - Red line on base the error (in the above code)

    According to your code snippet, it seems that we need to inherit DbContext class, like this:

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

    >>ERROR 2 - Red line below ProductDatabaseInitializer this one I have no idea why :(

    According to your code snippet, it's a custom database initialization, You can insert data into your database tables during the database initialization process. This will be important if you want to provide some test data for your application or to provide some default master data for your application. it seems ok. Do you encounter any issue? does it throw any exception?

    For more information about database initialization, please refer to:

    http://www.entityframeworktutorial.net/code-first/seed-database-in-code-first.aspx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 15, 2016 6:52 AM
    Moderator