简体   繁体   中英

Mapping composite poco to EF objectcontext entities

I have to produce an output from 3 separate tables(with a couple of fields from each table) into 1 output. I have a class that represents that output. The data is pulled from linq query of EF 6.1.x ObjectContext(Im stuck with using ObjectContext due to the nature of my clients needs....) entities (the 3 classes properly joined in the query) to a list of the new class (List<>). I populate a grid and all is fine. However the user wants to edit the data in the grid and now I need to push those new changes back.

My question is this: Can I map my new class back to the entities field to field? Or am I stuck with iterating through the collection and updating the tables individually? I thought I could map but I haven't run across anything that substantiates this.

Could you not do this using the "Proxy" pattern?

I've done a 2 entity + Wrapper example pseudo example below.

EF would "Save" the SuperWrapper.DeptProxy and the SuperWrapper.EmpProxy.

public partial class DepartmentEFEntity    {
    public virtual Guid? DepartmentUUID { get; set; }

    public virtual string DepartmentName { get; set; }
    public virtual ICollection<EmployeeEFEntity> Employees { get; set; }

}



public partial class EmployeeEFEntity
{

    public virtual Guid? ParentDepartmentUUID { get; set; }

    public virtual Guid? EmployeeUUID { get; set; }

    public virtual DepartmentEFEntity ParentDepartment { get; set; }

    public virtual string SSN { get; set; }
}



public class SuperWrapper
{

    internal DepartmentEFEntity DeptProxy { get; private set; }
    internal EmployeeEFEntity EmpProxy { get; private set; }

    public SuperWrapper(DepartmentEFEntity dept, EmployeeEFEntity emp)
    {
        this.DeptProxy = dept;
        this.EmpProxy = emp;
    }

    public string DepartmentName
    {
        get { return null == this.DeptProxy ? string.Empty : this.DeptProxy.DepartmentName; }
        set { if(null!=this.DeptProxy{this.DeptProxy.DepartmentName =value;}}
    }

    public string EmployeeSSN
    {
        get { return null == this.EmpProxy ? string.Empty : this.EmpProxy.SSN; }
        set { if(null!=this.EmpProxy{this.EmpProxy.SSN =value;}}
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM