dbset.Remove(entity)not working此行出错

时间:2015-07-10 21:22:10

标签: c# entity-framework model-view-controller

我的删除功能无效。

   public virtual void Delete(T entity)
   {
      dbset.Remove(entity);
   }

我首先实现了工作模式和存储库模式以及实体框架代码。我有代码第一个实体层,服务层,数据上下文层和UI层。我的编辑,更新,添加功能都很好。但我的删除功能不起作用。

我收到此错误。 类型' System.ArgumentNullException'的例外情况发生在EntityFramework.dll中,但未在用户代码中处理。

其他信息:值不能为空"值不能为null。参数名称:实体"

在我的项目控制器中。我的代码就是这样----

  public class ProjectController : Controller
   {
      private readonly IProjectService _projectService;
      public ProjectController(IProjectService projectService)
   {
      this._projectService = projectService;
   }

    [HttpGet]
    public ActionResult Delete(int? id)
    {
        var projectDetails = _projectService.GetProjectById((int)id);
        if (projectDetails == null) throw new ArgumentNullException("Not Found");
        return View(projectDetails);
    }
    [HttpPost]
    public ActionResult Delete(Project project)
    {
        _projectService.DeleteProject(project.ProjectId);
        return RedirectToAction("List","Project");
    }

在我的服务层,我拥有控制器实现的所有逻辑----

    public class ProjectService : IProjectService
      {
        private readonly IProjectRepository _projectRepository;
        private readonly IUnitOfWork _unitOfWork;
        public ProjectService(IProjectRepository           projectRepository,IUnitOfWork unitOfWork)                  
      {
       this._projectRepository = projectRepository;
       this._unitOfWork = unitOfWork;
   }

   public void DeleteProject(int id)
      {
       var project = _projectRepository.GetById(id);
       _projectRepository.Delete(project);
       _unitOfWork.Commit();
      }

我的实体存储库基类包含-------

的方法
   public abstract class EntityRepositoryBase<T> where T : class
     {
        private CodeFirstContext _dataContext;
        private readonly IDbSet<T> dbset;
        protected EntityRepositoryBase(IDBFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        dbset = DataContext.Set<T>();
    }

    protected IDBFactory DatabaseFactory
    {
        get;
        private set;
    }

    protected CodeFirstContext DataContext
    {
        get { return _dataContext ?? (_dataContext = DatabaseFactory.Get()); }
    }

    public virtual void Delete(T entity)
    {
        dbset.Remove(entity);
    }

    public void Delete(Func<T, Boolean> where)
    {
        IEnumerable<T> objects = dbset.Where<T>(where).AsEnumerable();
        foreach (T obj in objects)
        dbset.Remove(obj);
    }

在我的IEntityRepository类中,方法是-----

  public interface IEntityRepository<T> where T : class
    {
       void Delete(T entity);
       void Delete(Func<T, Boolean> predicate);
     }

我的Delete.cshtml文件是这样的----

    @model CodeFirstEntities.Models.Project

   @{
      ViewBag.Title = "Delete";
    }

   <h2>Delete</h2>

   <h3>Are you sure you want to delete this?</h3>
   <fieldset>
<legend>Project</legend> 
@Html.HiddenFor(model => model.ProjectId)
@Html.HiddenFor(model => model.ProjectName)
@Html.HiddenFor(model => model.DeveloperName)
@Html.HiddenFor(model => model.Address)
@Html.HiddenFor(model => model.Website)
@Html.HiddenFor(model => model.Geolongitude)
@Html.HiddenFor(model => model.Geolatitude)

<div class="display-label">
     @Html.DisplayNameFor(model => model.ProjectName)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.ProjectName)
</div>

<div class="display-label">
     @Html.DisplayNameFor(model => model.DeveloperName)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.DeveloperName)
</div>

<div class="display-label">
     @Html.DisplayNameFor(model => model.Address)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Address)
</div>

<div class="display-label">
     @Html.DisplayNameFor(model => model.Website)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Website)
</div>

<div class="display-label">
     @Html.DisplayNameFor(model => model.Geolongitude)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Geolongitude)
</div>

<div class="display-label">
     @Html.DisplayNameFor(model => model.Geolatitude)
</div>
<div class="display-field">
    @Html.DisplayFor(model => model.Geolatitude)
  </div>
    </fieldset>
     @using (Html.BeginForm()) {
     @Html.AntiForgeryToken()
     <p>
     <input type="submit" value="Delete" /> |
     @Html.ActionLink("List", "Project")
     </p>
       }

1 个答案:

答案 0 :(得分:0)

通过查看表单的标记,我看不到指定项目信息的位置。而是在项目表单之外指定项目信息。因此,如果进行以下更改,则可以在按下“删除”按钮时将项目信息发送到服务器。

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    <p>
        @Html.HiddenFor(model => model.ProjectId)
        @Html.HiddenFor(model => model.ProjectName)
        @Html.HiddenFor(model => model.DeveloperName)
        @Html.HiddenFor(model => model.Address)
        @Html.HiddenFor(model => model.Website)
        @Html.HiddenFor(model => model.Geolongitude)
        @Html.HiddenFor(model => model.Geolatitude)

        <input type="submit" value="Delete" /> |
        @Html.ActionLink("List", "Project")
    </p>
}