SQL Server Deleting object in Entity Framework by Id

r3i60tvu  于 2023-04-10  发布在  其他
关注(0)|答案(1)|浏览(135)

I'm trying to develop a simple c# MVVM CRUD application which hooks up to a SQL Server database, I'm currently having an issue deleting records of my entities Apparently the entityId I'm searching for returns null, in which I'm pretty much sure it exists in the database, Therefore no deleting happens I could really use you help! thank you!

public async Task<bool> Delete(int id)
    {
        using (MyDbContext context = _contextFactory.CreateDbContext())
        {
            T entity = await context.Set<T>().FirstOrDefaultAsync((e) => e.Id == id);
            context.Set<T>().Remove(entity);
            context.SaveChangesAsync();
            return true;
        }
    }
xggvc2p6

xggvc2p61#

Well I'm not getting any error, this is the code I'm running for deleting a user with Id = 2 :

IDataService<User> userService = new GenericDataService<User>(new MyDbContextFactory());
userService.Delete(2);

相关问题