Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC 4实体框架5不能删除多对多关系_C#_Asp.net Mvc 4_Entity Framework 5 - Fatal编程技术网

C# MVC 4实体框架5不能删除多对多关系

C# MVC 4实体框架5不能删除多对多关系,c#,asp.net-mvc-4,entity-framework-5,C#,Asp.net Mvc 4,Entity Framework 5,我使用的是EntityFramework5和MVC4代码优先的方法。我对这两个都很陌生,所以我可能犯了一个简单的错误,我试图从数据库中删除多对多关系。我可以很好地添加关系,但是当我想要删除它时,我会得到一个错误 我必须使用多对多关系的表格。我有员工等级和扣除等级: 按ICollection链接到扣减类别的员工类别: public class Employee { public int EmployeeID { get; set; } public string Name { g

我使用的是EntityFramework5和MVC4代码优先的方法。我对这两个都很陌生,所以我可能犯了一个简单的错误,我试图从数据库中删除多对多关系。我可以很好地添加关系,但是当我想要删除它时,我会得到一个错误

我必须使用多对多关系的表格。我有员工等级和扣除等级:

按ICollection链接到扣减类别的员工类别:

public class Employee
{

    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public int PhoneNumber { get; set; }
    public int CellNumber { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public int PostalCode { get; set; }
    public string BankName { get; set; }
    public int BankAccountNumber { get; set; }
    public string PayInterval { get; set; }
    public double NormalRate { get; set; }
    public double OvertimeRate { get; set; }
    public double SetHours { get; set; }
    public DateTime DateOfEmployment { get; set; }
    public DateTime LastPaymentRecieved { get; set; }
    public string Active { get; set; }
    public int JobID { get; set; }

    public ICollection<Holiday> holiday { get; set; }
    public ICollection<Bonus> bonus { get; set; }
    public ICollection<Absent> absent { get; set; }
    public Job Job { get; set; }
    public ICollection<Deduction> Deductions { get; set; }
    public ICollection<HoursWorked> HoursWorke { get; set; }
    public ICollection<Payroll> Payrolls { get; set; }
}
public class Deduction
{

    public int DeductionID { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public double Amount { get; set; }

    public ICollection<Employee> Employee { get; set; }
    public ICollection<Payroll> Payroll { get; set; }
}
我确实检查了员工和要删除的扣减是否正确提取。我注意到在employee.Decreations.Remove(ded)中,扣减项为空。 以下是错误:

我也尝试了多个关于堆栈溢出的答案,但似乎都不起作用,所以我的代码中的一个例子可能会对我有所帮助

提前谢谢

编辑

我已通过添加以下内容解决了此错误: 问题是我在检索员工时没有包括扣除额。它现在正在按预期工作

    public void RemoveEmployeeDeduction(int empID, int dedID)
    {
        var employee = db.Employee.Include("Deductions").FirstOrDefault(x => x.EmployeeID == empID);

        var ded = db.Deduction.FirstOrDefault(x => x.DeductionID == dedID);

        employee.Deductions.Remove(ded);
        db.SaveChanges();

    }

您应该在帖子中发布相关代码和错误(使用代码fprmat)。链接和外部图像可能会被破坏,因此不受欢迎。我假设您在尝试调用
Remove()
@TomToms之前也初始化了您的收藏。谢谢您的建议,我已经更改了我的帖子。@AgentShark是的,这似乎是我的问题,我通过添加.Include()解决了它。谢谢你的快速回复。
    public void RemoveEmployeeDeduction(int empID, int dedID)
    {
        var employee = db.Employee.Include("Deductions").FirstOrDefault(x => x.EmployeeID == empID);

        var ded = db.Deduction.FirstOrDefault(x => x.DeductionID == dedID);

        employee.Deductions.Remove(ded);
        db.SaveChanges();

    }