我想能打印收据时,在数据库中的东西是真实的。例如,爪切,它应该打印一行“这是您的收据”+爪。但是,当数据库中有ClawCutting true时,它仍然会打印"Error“。我真的不明白为什么它不能在数据库中找到它的真值?
public void DeleteFromService()
{
using (var db = new ApplicationDbContext())
{
double claw = 2.99;
double hair = 3.99;
double washing = 4.99;
Console.WriteLine("Add your booking number on your service");
Services s = new Services();
s.Id = Convert.ToInt32(Console.ReadLine());
if(s.ClawCutting == true)
{
Console.WriteLine("Here are your receipt" + claw);
}
else if(s.HairCut == true)
{
Console.WriteLine("Here are your receipt" + hair);
}
else if (s.Washing == true)
{
Console.WriteLine("Here are your receipt" + washing);
}
else
{
Console.WriteLine("Error");
}
db.Services.Remove(s);
db.SaveChanges();
}
}发布于 2021-12-20 16:54:29
您的代码从不从数据库中加载任何内容。Service是一个新对象,它的所有属性都有默认值。如果要加载特定项,请使用DbSet.Find,其中id是要加载的项的主键值。
var id=Convert.ToInt32(Console.ReadLine());
var s=db.Services.Find(id);
if(s == null)
{
Console.WriteLine($"No service with {id} was found");
return;
}这两种方法将从数据库中删除服务,因此,确保只在您真正想要的情况下才使用它们:
db.Services.Remove(s);
db.SaveChanges();https://stackoverflow.com/questions/70425230
复制相似问题