【问题标题】:Why is our product table empty?为什么我们的产品表是空的?
【发布时间】:2019-11-11 12:39:26
【问题描述】:

我和一个朋友正在做一个项目。当我们运行代码时,产品页面上没有数据。所有产品都不见了。

我们创建了一个DbInitializer.cs 文件,其中包含不同产品的数据。显示文件中的所有其他数据,但不显示产品数据。

 var products = new Product[]
           {
                new Product{ProductID=1050,ProductName="CD-ORD",Price=100,
                 MarketID = markets.Single( s => s.Name == "england").MarketID
                },


                new Product{ProductID=4022,ProductName="Intowords",Price=100,
                 MarketID = markets.Single( s => s.Name == "england").MarketID
                },
                new Product{ProductID=4041,ProductName="Reading Pen",Price=100,
                 MarketID = markets.Single( s => s.Name == "england").MarketID
                },
                new Product{ProductID=1045,ProductName="CD-ORD and Intowords",Price=150,
                 MarketID = markets.Single( s => s.Name == "england").MarketID
                },
                new Product{ProductID=3141,ProductName="CD-ORD and Reading Pen",Price=150,
                 MarketID = markets.Single( s => s.Name == "england").MarketID
                },
                new Product{ProductID=2021,ProductName="Intowords and Reading Pen",Price=150,
                 MarketID = markets.Single( s => s.Name == "england").MarketID
                },
                new Product{ProductID=2042,ProductName="Intowords",Price=100,
                 MarketID = markets.Single( s => s.Name == "england").MarketID
                },
                new Product{ProductID=2042,ProductName="CD-ORD and Intowords and Reading Pen",Price=175,
                 MarketID = markets.Single( s => s.Name == "england").MarketID
                },
           };
            foreach (Product p in products)
            {
                context.Products.Add(p);
            }
            context.SaveChanges();

更新数据库后,我们希望数据进入 Product 表并在我们运行代码时显示在网站上,但事实并非如此。这是整个项目的链接:https://github.com/ahma0307/VitekSite6

非常感谢您的帮助。我们已经研究了好几个小时了。

编辑:以下代码显示了我认为的对象映射

public class BusinessContext : DbContext
    {
        public BusinessContext (DbContextOptions<BusinessContext> options)
            : base(options)
        {
        }
        public DbSet<Product> Products { get; set; }
        public DbSet<Subscription> Subscriptions { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Market> Markets { get; set; }
        public DbSet<ProductGuide> ProductGuides { get; set; }
        public DbSet<CountryAssignment> CountryAssignments { get; set; }
        public DbSet<ProductAssignment> ProductAssignments { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>().ToTable("Product");
            modelBuilder.Entity<Subscription>().ToTable("Subscription");
            modelBuilder.Entity<Customer>().ToTable("Customer");
            modelBuilder.Entity<Market>().ToTable("Market");
            modelBuilder.Entity<ProductGuide>().ToTable("ProductGuide");
            modelBuilder.Entity<CountryAssignment>().ToTable("CountryAssignment");
            modelBuilder.Entity<ProductAssignment>().ToTable("ProductAssignment");

            modelBuilder.Entity<ProductAssignment>()
                .HasKey(pa => new { pa.ProductID, pa.ProductGuideID });
        }

        public DbSet<VitekSite.Models.Customer> Customer { get; set; }
    }

编辑 2:我更改了 ProductID,因此它们不同,并将所有 s.Name 大写

 var products = new Product[]
       {
            new Product{ProductID=1050,ProductName="CD-ORD",Price=100,
             MarketID = markets.Single( s => s.Name == "England").MarketID
            },
            new Product{ProductID=4022,ProductName="Intowords",Price=100,
             MarketID = markets.Single( s => s.Name == "England").MarketID
            },
            new Product{ProductID=4041,ProductName="Reading Pen",Price=100,
             MarketID = markets.Single( s => s.Name == "Danmark").MarketID
            },
            new Product{ProductID=1045,ProductName="CD-ORD and Intowords",Price=150,
             MarketID = markets.Single( s => s.Name == "Sverige").MarketID
            },
            new Product{ProductID=3141,ProductName="CD-ORD and Reading Pen",Price=150,
             MarketID = markets.Single( s => s.Name == "Norge").MarketID
            },
            new Product{ProductID=2021,ProductName="Intowords and Reading Pen",Price=150,
             MarketID = markets.Single( s => s.Name == "Danmark").MarketID
            },
            new Product{ProductID=2042,ProductName="Intowords",Price=100,
             MarketID = markets.Single( s => s.Name == "Norge").MarketID
            },
       };

【问题讨论】:

  • “产品页面上没有数据” - 但是数据库中有产品数据吗?
  • 这可能意味着一些不同的问题。首先,正如@stuartd 提到的,您可以在加载项目后Select SQL Server 数据库中的数据吗?其次,问题可能出在显示数据的页面上吗?
  • 欢迎来到 StackOverflow。首先,请将代码发布到相关区域,而不是整个项目的链接。其次,对于这段代码,不要执行 foreach 循环,而是执行 context.AddRange(products);
  • 您还必须包含对象映射,并确保 SaveChanges() 不会静默失败。
  • @suartd 不,数据库中没有我不理解的产品数据,因为客户表中有客户数据

标签: c# sql-server asp.net-core visual-studio-2019


【解决方案1】:

那是因为您添加了两个具有相同 id 的产品。请确保 s.Name == "England" 不是 s.Name == "england"

要修复它,请更改:

new Product{ProductID=2042,ProductName="CD-ORD and Intowords and Reading Pen",Price=175,
             MarketID = markets.Single( s => s.Name == "England").MarketID
            },

到:

new Product{ProductID=2043,ProductName="CD-ORD and Intowords and Reading Pen",Price=175,
             MarketID = markets.Single( s => s.Name == "England").MarketID
            },

更新: Product 的 MarketID 应该存在于 Market 中。您在 Market 中没有带有 Name=Norge 的对象。

要修复它,请更改:

new Product{ProductID=3141,ProductName="CD-ORD and Reading Pen",Price=150,
             MarketID = markets.Single( s => s.Name == "Norge").MarketID
            },              
            new Product{ProductID=2042,ProductName="Intowords",Price=100,
             MarketID = markets.Single( s => s.Name == "Norge").MarketID
            },

收件人:

new Product{ProductID=3141,ProductName="CD-ORD and Reading Pen",Price=150,
             MarketID = markets.Single( s => s.Name == "Danmark").MarketID
            },              
            new Product{ProductID=2042,ProductName="Intowords",Price=100,
             MarketID = markets.Single( s => s.Name == "Danmark").MarketID
            },

另一种解决方法是您可以在 Market 中添加记录:

new Market { Name = "Norge",   Budget = 200000,
                StartDate = DateTime.Parse("2007-09-01"),
                ProductGuideID  = productGuides.Single( pg => pg.LastName == "Kapoor").ID }

【讨论】:

  • 谢谢。我已使用 EDIT 2 更新了原始帖子,其中显示了您提到的更改。我的 ProductID 现在不同了,所有 s.Name 都以大写字母开头。但是我的数据库中的产品表仍然是空的,因此当我运行调试器时,产品视图也是空的。
  • 我已经测试了你在 github 中的代码。请检查我的答案。如果它适合你,你能accept我的答案吗?
  • 修改代码后,请务必删除数据库,再次使用命令update-database运行项目。
  • 答案被接受它有效。非常感谢。我们花了大约 8 个小时试图修复它。
猜你喜欢
  • 2021-08-19
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
  • 2012-07-30
相关资源
最近更新 更多