实体框架 - 数据库表加入Linq to Entities

时间:2013-12-02 12:36:22

标签: asp.net-mvc linq entity-framework join linq-to-entities

我有两个名为employee和employee detail的数据库表。我使用entityframwork。

我想制作一个包含两个表格字段的编辑页面。现在,当我点击网格视图(包含表格的详细信息)时,我传递了主要ID,并根据它进行连接。

加入详情如下

var list = (from g in db.Employee
                   join d in db.db.EmployeeDetail on g.EGID equals d.EGID
                   where g.EGID == id
                   select new CombineModel
                     {
                          EE_GENERAL = g,
                          EE_DEMOGRAPHIC = d
                     });

上面的代码" CombineModel" class包含具有两个表字段的属性。

public class CombineModel
{
    public Employee EE_GENERAL { get; set; }
    public EmployeeDetail EE_DEMOGRAPHIC { get; set; }

}

现在,当我通过" list"返回我的编辑页面的视图时它显示我的错误,我无法获取我的加入数据。我的编辑视图与combinemodel绑定。

请指导我如何输入以上列表或获取列表中的详细信息。

以下是员工类

 [MetadataType(typeof(Employee_Validation))]
public partial class Employee
{

}

public class Employee_Validation
{
    public Decimal EGID { get; set; }
    public Decimal CID { get; set; }

    [StringLength(20)]
    //[Required(ErrorMessage = "Employee ID is required")]
    public String EmployeeID { get; set; }

    [Required(ErrorMessage = "Effective Date is required")]
    [DataType(DataType.Date)]
    public DateTime EffectiveDate { get; set; }

    public DateTime AddDate { get; set; }

    public String AddOper { get; set; }

    public DateTime ChgDate { get; set; }

    public String ChgOper { get; set; }

    [StringLength(30)]
    [Required(ErrorMessage = "Last Name is required")]
    public String LastName { get; set; }

    [StringLength(30)]
    [Required(ErrorMessage = "First Name is required")]
    public String FirstName { get; set; }


    [StringLength(15)]
    public String MiddleName { get; set; }

    [StringLength(50)]
    public String CheckName { get; set; }

    public Int32 SSNO { get; set; }

    [Required(ErrorMessage = "Please select atleast one security group.")]
    public String SecurityGroup { get; set; }

    [Required(ErrorMessage = "Please select atleast one security level.")]
    public Byte SecurityLevel { get; set; }

    public Byte EESequence { get; set; }

    [Required(ErrorMessage = "Change Type is required")]
    public Byte ChangeType { get; set; }

    [StringLength(200)]
    public String OptionalComments { get; set; }

}

以下是EmployeeDetail类

 [MetadataType(typeof(EmployeeDetail_Validation))]
public partial class EmployeeDetail
{ 

}

public class EmployeeDetail_Validation
{

    public Decimal DemographicId { get; set; }
    public Decimal CID { get; set; }
    public Decimal EGID { get; set; }

    public DateTime EffectiveDate { get; set; }
    public DateTime AddDate { get; set; }
    public String AddOper { get; set; }
    public DateTime ChgDate { get; set; }
    public String ChgOper { get; set; }

    [Required(ErrorMessage = "Birthdate is required")]
    [DataType(DataType.Date)]
    public DateTime Birthdate { get; set; }

    [StringLength(40)]
    public String NickName { get; set; }

    [StringLength(100)]
    [Required(ErrorMessage = "Address1 is required")]
    public String Address1 { get; set; }

    [StringLength(100)]
    public String Address2 { get; set; }

    [StringLength(100)]
    public String Address3 { get; set; }

    [StringLength(100)]
    [Required(ErrorMessage = "City is required")]
    public String City { get; set; }

    [Required(ErrorMessage = "Please select atleast one State.")]
    [StringLength(2)]
    public String State { get; set; }

    public String Zip { get; set; }

    [Required]
    [StringLength(200)]
    [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]
    [Display(Name = "Email address*")]
    public String EmailAddress { get; set; }

    [Required(ErrorMessage = "Please select atleast one Gender.")]
    public String Gender { get; set; }

    [Required(ErrorMessage = "Please select atleast one Marital Status.")]     
    public String MaritalStatus { get; set; }


    [StringLength(200)]
    public String SpouseName { get; set; }

    [DataType(DataType.Date)]
    public DateTime SpouseBirth { get; set; }

    public Int32 SpouseSSNO { get; set; }

    public Int32 EEOEthnic { get; set; }

    [Required(ErrorMessage = "Please select atleast one EEOJob.")]
    public Int32 EEOJob { get; set; }

    [Required(ErrorMessage = "Please select atleast one Citizen.")]  
    public Boolean Citizen { get; set; }

    public Decimal HomePhone { get; set; }

    [Range(0,int.MaxValue,ErrorMessage="Please enter proper MobilePhone.")]
    public Decimal MobilePhone { get; set; }

    public Decimal OfficePhone { get; set; }

    [StringLength(10)]
    public String OfficeExt { get; set; }

    [Range(0,int.MaxValue,ErrorMessage="BldgNo is too longer.")]
    [Required(ErrorMessage = "Building Number is required")]
    public Decimal BldgNo { get; set; }

    [StringLength(200)]
    [Required(ErrorMessage = "Emergency Contact is required")]
    public String EmergContact { get; set; }

    public Decimal EmergPhone { get; set; }

    [Required(ErrorMessage = "No Of Childeren is required")]
    public Int32 NoOfChilderen { get; set; }


}

1 个答案:

答案 0 :(得分:1)

请尝试以下

var list = (from g in db.Employee
   join d in db.db.EmployeeDetail on g.EGID equals d.EGID
   where g.EGID == id
   select new CombineModel
     {
          EE_GENERAL.PropertyName = g.ColumnName,
          EE_DEMOGRAPHIC.PropertyName = d.ColumnName
     });