简体   繁体   English

将复合poco映射到EF objectcontext实体

[英]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. 我必须从3个单独的表(每个表有几个字段)产生一个输出到1个输出。 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<>). 数据从EF 6.1.x的linq查询中拉出ObjectContext(由于我的客户需求的性质而被困在使用ObjectContext的情况下...)实体(在查询中正确连接的3个类)到新类的列表中(列表<>)。 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. 我在下面做了一个2实体+包装器示例的伪示例。

EF would "Save" the SuperWrapper.DeptProxy and the SuperWrapper.EmpProxy. EF将“保存” SuperWrapper.DeptProxy和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;}}
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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