I want to be able to print a receipt when something is true in the database. For example, clawcutting, it should print the line "Here are your receipt" claw. But when I have ClawCutting true in the database, it still prints "Error". I do not really understand why it can not find it true in database?
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();
}
}
CodePudding user response:
Your code never loads anything from the database. Service
is a new object and all its properties will have default values. If you want to load a specific item use DbSet.Find where id
the primary key value of the item you want to loadeg
var id=Convert.ToInt32(Console.ReadLine());
var s=db.Services.Find(id);
if(s == null)
{
Console.WriteLine($"No service with {id} was found");
return;
}
These two methods will delete the Service from the database, so make sure you use them only if that's what you really want :
db.Services.Remove(s);
db.SaveChanges();