Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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# 父子表,为现有父记录添加子记录错误地添加新父记录,然后添加子记录_C#_.net_Entity Framework_Asp.net Mvc 4_Entity Framework 5 - Fatal编程技术网

C# 父子表,为现有父记录添加子记录错误地添加新父记录,然后添加子记录

C# 父子表,为现有父记录添加子记录错误地添加新父记录,然后添加子记录,c#,.net,entity-framework,asp.net-mvc-4,entity-framework-5,C#,.net,Entity Framework,Asp.net Mvc 4,Entity Framework 5,在EF中,我有两个表,父表是Office,子表是Employee,我正在引用现有的Office记录向Employee添加记录,但我猜,由于我的配置错误,EF的行为已经厌倦了 它不使用Office表的现有记录,而是在Office中添加新记录,并在子记录中使用新记录的id作为外键,然后创建子记录 以下是我的模型: public class Office { public int OfficeId{ get; set; } public string Name

在EF中,我有两个表,父表是Office,子表是Employee,我正在引用现有的Office记录向Employee添加记录,但我猜,由于我的配置错误,EF的行为已经厌倦了

它不使用Office表的现有记录,而是在Office中添加新记录,并在子记录中使用新记录的id作为外键,然后创建子记录

以下是我的模型:

public class Office
    {
        public int OfficeId{ get; set; }
        public string Name { get; set; }           
        public virtual IEnumerable<Employee> Employees{ get; set; }

    }


 public class Employee
    {
        public int EmployeeID{ get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DOB { get; set; }
        [NotMapped]
        public int Age { get { return DOB.YearsFromUtcDateTillToday(); } }
        [NotMapped]
        public string FullName { get { return string.Concat(FirstName, ' ', LastName); } }
        public virtual Office Office { get; set; }

    }
我的控制器操作如下:

 public virtual ActionResult AddEmployee(int officeId)
        {
            Employee st = new Employee();
            st.Office=new Office();
            st.Office.OfficeID= officeId;
            return View(st);
        }

        [HttpPost]
        public virtual ActionResult AddEmployee(Employee objEmployee)
        {
           bool success= classEmployeeService.AddEmployee(objEmployee);
            if (success)
            {
                return RedirectToAction("Index",objEmployee.Office.OfficeId);
            }
            else
            {
                ModelState.AddModelError("", "Error! Business rule violation, can't repeat surname");
            }
            return View(objEmployee);

        }
我的发现: 在没有HttpPost OfficeId的第一个操作中是正确的,在本例中为1 检查html我才知道它是正确的 但当按下保存按钮执行HttpPost的第二个操作时,OfficeId值是错误的,它是新的Office记录id 18、19或下一个记录


请指导和帮助我。

因为我发现您的模型设计有问题。Employee类应该有一个属性来表示OfficeID的外键

public class Employee
    {
        public int EmployeeID{ get; set; }
        public int OfficeID{ get; set; } // This what you need
        // ...... the rest of class properties 
    }
然后只需将Office Id分配给此属性,而不是导航属性Office

Employee st = new Employee();    
st.OfficeID= officeId;
不要使用st.Office=新办公室;因为这会将此对象添加到DbContext StateManager中,并将其作为新记录添加到数据库中

Employee st = new Employee();    
st.OfficeID= officeId;