我的项目有问题。我在我的项目中使用Entity Framework Code First。
问题在于:我有两个模型,它们之间有一对多关系。
员工模型:
{
public int employeeId { get; set; }
public string name { get; set; }
public string surname { get; set; }
public int tel_no { get; set; }
public string fullname
{
get
{
return name + " " + surname;
}
}
public virtual Department Department { get; set; }
public virtual Manager Manager { get; set; }
}
部门模型:
{
public int departmentId { get; set; }
public string name { get; set; }
public virtual ICollection<Employee> Employee { get; set; }
}
首先,我成功地将部门数据插入部门表。之后,我想将员工数据插入员工表。当我将员工数据插入相关表时,我也从部门表中获取部门数据,以建立员工与数据之间的关系。
但是当我点击保存按钮时,我从department表中获得的数据再次被插入到department表中,而department表中有两个相同的数据。
以下是我的观点:
<div class = "form-group">
<label class = "control-label col-md-3"> Adı </label>
<div class = "col-md-8">
@Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3"> Soyadı </label>
<div class="col-md-8">
@Html.EditorFor(model => model.surname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.surname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3"> Telefon Numarası </label>
<div class="col-md-8">
@Html.EditorFor(model => model.tel_no, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.tel_no, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3"> Bölümü </label>
<div class="col-md-8">
@Html.DropDownListFor(model => model.Department.name, ViewBag.Departments as SelectList, new { @class = "form-control dropdown-toggle" })
@Html.ValidationMessageFor(model => model.Department.name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3"> Yöneticisi </label>
<div class="col-md-8">
@Html.DropDownListFor(model => model.Manager.fullname, ViewBag.Managers as SelectList, new { @class = "form-control dropdown-toggle" })
@Html.ValidationMessageFor(model => model.Manager.fullname, "", new { @class = "text-danger" })
</div>
</div>
这是控制器:
// GET: Admin/CreateEmployee
public ActionResult CreateEmployee()
{
var nameOfManagers = (from manager in database.Employee select manager);
var nameOfDepartments = (from department in database.Department select department);
ViewBag.Managers = new SelectList(nameOfManagers, "fullname", "fullname");
ViewBag.Departments = new SelectList(nameOfDepartments, "name", "name");
return View();
}
// POST: Admin/CreateEmployee
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateEmployee(Employee employee)
{
if(ModelState.IsValid)
{
if(employee.Manager != null)
{
String[] manager_info = employee.Manager.fullname.Split(' ');
for (int i = 0; i < manager_info.Length - 1; i++)
{
employee.Manager.name += manager_info[i] + " ";
}
employee.Manager.surname = manager_info[manager_info.Length - 1];
}
database.Employee.Add(employee);
database.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
最后一件事是正在运行的项目的截图:
Showing of Department and Employee Table data
我无法解决这个问题。如果你帮助我,我很高兴。 谢谢。
答案 0 :(得分:0)
问题是你真的在创建一个新的部门。通过发布到Department.name
,模型绑定器将在Department
上新建Employee
的实例,然后设置该实例的name
属性。但是,没有设置其他属性,重要的是,id。因此,当您保存员工时,没有ID的部门看起来是Entity Framework的新手,它也会创建它。
您应该做什么的是发布到Employee
上的某个属性,该属性不会导致Department
的实例被创建。基本上,您有两种选择:
如果Employee
上有明确的外键属性,例如DepartmentId
,请将发送到。如果填写了外键属性并且导航属性Department
为空,则实体框架将使用与数据库中的DepartmentId
匹配的部门回填它。
如果您没有明确的外键属性,即您只拥有Department
属性,并且依赖于Entity Framework的基于约定的方法来创建列该表自动跟踪该关系,然后您必须使用视图模型。让我再说一遍:你绝对不能直接使用你的实体。您需要创建类似的内容:
public class EmployeeViewModel
{
// other employee properties you want to edit
public int DepartmentId { get; set; }
}
然后,一旦您完成了帖子操作,您就需要将此视图模型中的值映射到您的实际实体,即使用DepartmentId
的已发布值来查找正确的部门从数据库中为您的员工设置Department
属性。