1 c# sql-delete entity-framework-core
我想根据AdvertId里面的来删除,而不是根据ID来删除。我找到了AdvertId,但我卡在删除上。我正在使用 CQRS 结构。我的文件夹Repository和IRepository文件夹是分开的。在控制器部分,我通过查找advertId.
public class Advert_CategoryPropertyDetailJunctionDeleteHandler :
IRequestHandler<Advert_CategoryPropertyDetailJunctionDelete, ApiResponse>
{
private readonly IUnitOfWork _repo;
public Advert_CategoryPropertyDetailJunctionDeleteHandler(IUnitOfWork repo)
{
_repo = repo;
}
public async Task<ApiResponse> Handle(Advert_CategoryPropertyDetailJunctionDelete
request, CancellationToken cancellationToken)
{
var mapped = await
_repo.AdvertCategoryPropertyDetailJunctions.GetAllAsync(request.predicate);
if (mapped == null)
return new ErrorApiResponse(ResultMessage.NotDeletedUser);
await _repo.AdvertCategoryPropertyDetailJunctions.DeleteAllAsync((Advert_CategoryPropertyDetailJunction) mapped);
return new SuccessApiResponse();
}
}
Run Code Online (Sandbox Code Playgroud)
红外存储库:
Task<IEnumerable> DeleteAllAsync(T entity);
Run Code Online (Sandbox Code Playgroud)
存储库;
public async Task<IEnumerable> DeleteAllAsync(T entity)
{
Context.Set<T>().Remove(entity);
Context.Entry(entity).State = EntityState.Deleted;
await Context.SaveChangesAsync();
return (IEnumerable)entity;
}
Run Code Online (Sandbox Code Playgroud)
此功能在此处描述的 EF7 中引入。
它看起来像:
await context.Customers
.Where(x => x.AdvertId == advertIdToDelete)
.ExecuteDeleteAsync();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2994 次 |
| 最近记录: |