【问题标题】:Cannot get my database-update to work无法让我的数据库更新工作
【发布时间】:2018-08-23 10:53:41
【问题描述】:

我已经推出了我的测试版网站。下一个版本应包含购物车和信用卡结帐。在制作这个购物车的过程中,我发现有几种不同价格的旧 Product-class 根本不起作用。我需要一个具有一个身份的价格或一个具有多个价格的子类映射到原始类(我将使用它):

       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; }

    }

我有很多研究构建了两个类:

   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; }

   }

我有这个 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);

    }
   }

我正在尝试编写一个种子类:

      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>()
                });
               }
             }

但我在尝试创建的产品的红色下划线中遇到以下问题:“没有给出与 Product.Product(ICollection) 所需的形式参数“价格”相对应的参数”。

【问题讨论】:

    标签: sql-server database asp.net-core-mvc entity-framework-core


    【解决方案1】:

    我相信您的Product 类中需要无参数构造函数。

    【讨论】:

    • 是的,这就是问题所在!谢谢!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多