LINQ vs Fluent API issue
Hello, smart people, I'm learning to make web apps with EF Core and was practicing on some simple CRUD operations. Trying to delete an object from my database in this case. I've ran into an issue where my code is working with Fluent API syntax and wont work with LINQ.
Here are code examples I am using:
Fluent API
var task = context.Tasks
.Where(t => t.Id == id)
.FirstOrDefault();
if (task is TaskToDo)
{
context.Remove(task);
context.SaveChanges();
}
LINQ
var task = from t in context.Tasks
where t.Id == id
select t;
foreach (var t in task)
{
context.Remove(t);
context.SaveChanges();
}
Since I'm just learning I don't even know what to check. Only thing I could check is that my task
variable in Fluent API and t
variable in LINQ task
query are both successfully recognized as my custom model entity. Screenshots attached.
Would appreciate any help figuring out my issue.
Thanks