首页 > 解决方案 > 如何使用mvc中的entityframework从表中删除所有记录

问题描述

我想删除表的所有记录。我可以根据 id 删除,但我不想基于 id。我的 sql server 表名是[EnergyMonitoringDB].[dbo].[Energies]如何删除它。

编码:

public string Delete_AllEnergyData() {
          using (EnergyMonitoringDBEntities Obj = new EnergyMonitoringDBEntities())
          {
                Obj.Energies.RemoveRange(Obj.Energies);
                Obj.SaveChanges();
                return "Energy Data Deleted Successfully";
          }
}

AngularJS 代码:

$scope.DeleteAll = function () {
       $http({
           method: "post",
           url: "http://localhost:3523/Admin/Home/Delete_AllEnergyData",
           datatype: "json",
           data: JSON.stringify(Emp)
       }).then(function (response) {
           alert(response.data);
           $scope.GetAllData();
       })
   };

html代码:

<input type="button" class="btn btn-danger" value="Delete All" ng-click="DeleteAll()" />

标签: c#angularjsentity-frameworkmodel-view-controller

解决方案


Delete_AllEnergyData没有任何参数。所以删除发送参数data: JSON.stringify(Emp)到控制器如下:

$scope.DeleteAll = function () {
        $http({
            method: "post",
            url: "http://localhost:3523/Admin/Home/Delete_AllEnergyData",
        }).then(function (response) {
            alert(response.data);
            $scope.GetAllData();
        })
    };

推荐阅读