【发布时间】: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 提到的,您可以在加载项目后
SelectSQL Server 数据库中的数据吗?其次,问题可能出在显示数据的页面上吗? -
欢迎来到 StackOverflow。首先,请将代码发布到相关区域,而不是整个项目的链接。其次,对于这段代码,不要执行 foreach 循环,而是执行 context.AddRange(products);
-
您还必须包含对象映射,并确保
SaveChanges()不会静默失败。 -
@suartd 不,数据库中没有我不理解的产品数据,因为客户表中有客户数据
标签: c# sql-server asp.net-core visual-studio-2019