locked
Cannot get my database-update to work RRS feed

  • Question

  • I have launched my website in beta-version. The next version should contain a shopping cart and a checkout with the credit card. On my way to making this shopping cart, I've discovered that my old Product-class with several different prices simply doesn't work. I need one price with one identity or a subclass with several prices mapped to the original class(which I will use) :
               public class Product
            {
            [Key]
            public int ProductID { get; set; }
            [Required(ErrorMessage = "Please enter an product name")]
            public string Name { get; set; }
            [Required(ErrorMessage = "Please specify a category")]
            public string Category { get; set; }
            public string SubCategory { get; set; }
            public string Description { get; set; }
            public decimal Price16 { get; set; }
            public decimal Price12 { get; set; }
            public decimal Price8 { get; set; }
            public decimal Price4 { get; set; }

            public decimal PriceEach { get; set; }
            public decimal PriceKg { get; set; }
            public string ProductImageSmallUrl { get; set; }
            public string ProductImageSmallAlternativeDescription { get; set; }
            public string ProductImageSmallContentType { get; set; }
            public string ProductImageLargeUrl { get; set; }
            public string ProductImageLargeAlternativeDescription { get; set; }
            public string ProductImageLargeContentType { get; set; }
            public string ProductImageLargeSecondUrl { get; set; }
            public string ProductImageLargeSecondAlternativeDescription { get;
            set; }
            public string ProductImageLargeSecondContentType { get; set; }
            }

    I have after, a lot of research constructed two classes:
           public class Product
           {
            public Product(ICollection<Price> prices)
            {
                Prices = prices;
            }
            [Key]
            public int ProductID { get; set; }
            [Required(ErrorMessage = "Please enter an product name")]
            public string Name { get; set; }
            [Required(ErrorMessage = "Please specify a category")]
            public string Category { get; set; }
            public string SubCategory { get; set; }
            public string Description { get; set; }
            public string ProductImageSmallUrl { get; set; }
            public string ProductImageSmallAlternativeDescription { get; set; }
            public string ProductImageSmallContentType { get; set; }
            public string ProductImageLargeUrl { get; set; }
            public string ProductImageLargeAlternativeDescription { get; set; }
            public string ProductImageLargeContentType { get; set; }
            public string ProductImageLargeSecondUrl { get; set; }
            public string ProductImageLargeSecondAlternativeDescription { get;
           set; }
            public string ProductImageLargeSecondContentType { get; set; }
            public ICollection<Price> Prices { get; set; }
           }
          And a price class:
          public class Price
           {
            [Key]
            public int ID { get; set; }
            public int CurrentProductID { get; set; }
            public string Size { get; set; }
            public decimal Value { get; set; }
            public Product CurrentProduct { get; set; }
           }
    I have this DbContext:

            public class ApplicationDbContext : DbContext
            {
            public ApplicationDbContext(DbContextOptions<MokaMokkaDbContext> 
           options)
                :base(options) {}
            public DbSet<Product> Products { get; set; }
            public DbSet<Price> Prices { get; set; }
            protected override void OnModelCreating (ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Price>()
                    .HasOne(p => p.CurrentProduct)
                    .WithMany(b => b.Prices)
                    .HasForeignKey(p => p.CurrentProductID);
            }
           }
    I am trying to write a seeding class:
              public class SeedData
              {
                public static EnsurePopulated(IApplicationBuilder app)
                {
                MokaMokkaDbContext context = app.ApplicationServices
                    .GetRequiredService<MokaMokkaDbContext>();
                context.Database.Migrate();
                if(!context.Products.Any())
                {
                    context.Products.AddRange(
                        new Product
                        {
                            Name = "Dobos cake",
                            Category = "Cake",
                            ProductImageSmallUrl = "Dobos.Torta.jpg",
                            ProductImageSmallContentType = "jpg",
                            Prices = new List<Price>()
                        });
                       }
                     }
    But I get the following problem in over the red underline of the Product I am trying to create: "There is no argument given that corresponds to the required formal parameter "prices" of Product.Product(ICollection<Price>)".
    • Moved by CoolDadTx Thursday, March 15, 2018 1:43 PM EF related
    Thursday, March 15, 2018 8:45 AM

All replies

  • Yeah it's the price you pay in using EF code first.

    Anyway, EF issues can be discussed at the EF forum.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    Thursday, March 15, 2018 9:30 AM
  • Hi Harry_Xenon,

    Do you use entity framework core? please try to change your seed method like below and check if it works for you.

    public static EnsurePopulated(IApplicationBuilder app)
                 {
                 MokaMokkaDbContext context = app.ApplicationServices
                     .GetRequiredService<MokaMokkaDbContext>();
                 context.Database.Migrate();
                 if(!context.Products.Any())
                 {
                     context.Products.AddRange(
                         new Product(new List<Price>()
    )
                         {
                             Name = "Dobos cake",
                             Category = "Cake",
                             ProductImageSmallUrl = "Dobos.Torta.jpg",
                             ProductImageSmallContentType = "jpg",
                             });
                        }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 16, 2018 1:49 AM