开发者

Error "Value cannot be null. Parameter name: entity" when deleting entity object

I'm getting a strange error when I try to delete an entity. I'm using Entity Framework 4, C# and ASP.NET MVC 3.

Here's my controller and the function to delete an entity object:

public class EmployeesController : Controller
{
    readonly IEmployeesRepository _employeesRepository;

    public EmployeesController()
    {
        _employeesRepository = new SqlEmployeesRepository();
    }

    public RedirectToRouteResult Delete(int id)
    {
        var employee = _employeesRepository.GetEmployee(id);
        _employeesRepository开发者_如何学JAVA.DeleteEmployee(employee);
        TempData["message"] = employee.Name + " was deleted";
        return RedirectToAction("Index");
    }
}

And the repository code:

public class SqlEmployeesRepository : IEmployeesRepository
{
    private readonly MyDBEntities _entities;

    public SqlAgencyTypesRepository()
    {            
        _entities = new MyDBEntities();
    }


    public IQueryable<Employee> Employees
    {
        get { return _entities.Employees.AsQueryable(); }
    }


    public Employee GetEmployee(int id)
    {
        return Employees.FirstOrDefault(e => e.EmployeeID == id);
    }


    public void DeleteEmployee(Employee employee)
    {
        _entities.Employees.Context.DeleteObject(employee);
        _entities.Employees.Context.SaveChanges();
    }
}

A couple weird things:

  • The item does get deleted. An error occurs at the DeleteObject line, but it's still deleted.
  • This doesn't happen on my local machine, only on production. The only difference between the two is the connection string.

    <!-- Local conn string -->
    <!--<add name="MyDBEntities" connectionString="metadata=res://*/Entities.MyDB.csdl|res://*/Entities.MyDB.ssdl|res://*/Entities.MyDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />-->
    
    <!-- Production conn string -->
    <add name="MyDBEntities" connectionString="metadata=res://*/Entities.MyDB.csdl|res://*/Entities.MyDB.ssdl|res://*/Entities.MyDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;Server=mysite.com;Database=MyDB;User ID=***;Password=***;Trusted_Connection=True;Integrated Security=False;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    

Anybody know what's happening here?


Use Single instead of FirstOrDefault in GetEmployee. This will help to rule out issues associated with there being no record with a matching ID.


In my opinion You should use: Remove() instead of Context.DeleteObject()

_entities.Employees.Remove(employee); _entities.SaveChanges();

Also check if Your "Employee" doesn't have foreign key references which could prevent deleting this object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜