直接选择数据与使用EF中的导航属性

时间:2016-09-09 11:13:15

标签: asp.net-mvc entity-framework

您好在MVC应用程序中我有以下示例POCO类(实际应用程序有10-15个员工数据屏幕)。经理有很多员工,他可以更新他们的个人和联系方式。我正在考虑使用EF从数据库中检索当前员工数据的3种方法。 1)当经理选择员工时,在会话中存储EmployeeId(Session [“currentEmp”])并使用此EmployeeId获取当前员工的数据。

像:

int employeeId = (int)Session["currentEmp"];
EmployeeContactDetails1 empCtDet1 = ctx.EmployeeContactDetails1.Find(employeeId);
then 
EmployeeContactDetails2 empCtDet2 = ctx.EmployeeContactDetails2.Find(employeeId);

2)在Employee类上有一个鉴别器属性,例如“unlocked”,当经理选择员工然后将其标记为unlocked = true并更新数据库中的列并执行类似的操作以进行进一步的数据检索

EmployeeContactDetails1 empCtDet1 = ctx.Employees.Where(e => e.unlocked == true).EmployeeContactDetails1;
EmployeeContactDetails2 empCtDet2 = ctx.Employees.Where(e => e.unlocked == true).EmployeeContactDetails2;

3)或

EmployeeContactDetails1 empCtDet1 = ctx.EmployeeContactDetails1.Where(e => e.Employee.unlocked == true).FirstOrDefault();
    EmployeeContactDetails2 empCtDet2 = ctx.EmployeeContactDetails2.Where(e => e.Employee.unlocked == true).FirstOrDefault();

我想问你哪个更好,牢记安全和性能。

public class Manager{
public int ManagerId{get;set;}
public string ManagerName{get;set;}
public string someMoreProp{get;set;}
public ICollection<Employee>Employee{get;set;}
}

public class Employee{
public int EmployeeId{get;set;}
public virtual EmployeePersonalDetails EmployeePersonalDetails{get;set;}
public virtual EmployeeContactDetails EmployeeContactDetails{get;set;}

public int ManagerId{get;set;}
public virtual Manager Manager{get;set;}
}


   public class EmployeePersonalDetails{
    public int EmployeeId{get;set;}
    public string Name{get;set;}
    public string Age{get;set;}
    public Address AddressOne{get;set;}
    public Employee Employee{get;set;}
    }

    public class EmployeeContactDetails1{
    public int EmployeeId{get;set;}
    public string Line1{get;set;}
    public string Line2{get;set;}
public Employee Employee{get;set;}
    }

    public class EmployeeContactDetails2{
    public int EmployeeId{get;set;}
    public string Line1{get;set;}
    public string Line2{get;set;}
public Employee Employee{get;set;}
    }
    public class EmployeeContactDetails3{
    public int EmployeeId{get;set;}
    public string Line1{get;set;}
    public string Line2{get;set;}
public Employee Employee{get;set;}
    }

    public class EmployeeContactDetails4{
    public int EmployeeId{get;set;}
    public string Line1{get;set;}
    public string Line2{get;set;}
    public Employee Employee{get;set;}
    }

1 个答案:

答案 0 :(得分:0)

首先,不要使用Session。您提到的上述方式是完全不可接受的会话使用。一个精心设计的网络应用程序将尽可能避免像瘟疫这样的会话。值得注意的例外当然是身份验证,无法通过任何其他方式以用户友好的方式实现。

其次,一般来说,你似乎太过复杂了。目前还不完全清楚你想要实现的目标,但听起来你只是希望经理能够做一些事情,比如点击一个员工然后被带到一个他们可以编辑该员工细节的视图。正确的方法是拥有一个专用URI,由员工ID或其他唯一标识令牌组成。例如:

/employees/{id}/edit

然后,在该URI路由到的操作中,您将使用id param来查找该员工:

var employee = db.Employees.Find(id);
if (employee == null)
{
    return new HttpNotFoundResult();
}

因为,您可能只希望该员工的经理能够编辑该员工,您只需通过在查询中包含经理的ID来进行对象级授权:

var managerId = User.Identity.GetUserId(); // currently logged in manager
var employee = db.Employees.SingleOrDefault(m => m.EmployeeId == id && m.ManagerId == managerId);