我希望能夠在資料庫中的某些內容為真時列印收據。例如,爪切,它應該列印“這是您的收據” 爪。但是當我在資料庫中設定 ClawCutting 為 true 時,它??仍然列印“錯誤”。我真的不明白為什么它在資料庫中找不到它?
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();
}
}
uj5u.com熱心網友回復:
您的代碼永遠不會從資料庫中加載任何內容。Service是一個新物件,其所有屬性都將具有默認值。如果要加載特定專案,請使用 DbSet.Find where 要加載的專案id的主鍵值eg
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://www.uj5u.com/gongcheng/387472.html
上一篇:如何將兩張圖片放入一張圖片中
