简体   繁体   English

实体框架6投影,POCO实体返回类型

[英]Entity Framework 6 Projection, POCO entity return type

Context: I have a solution with 3 projects 上下文:我有3个项目的解决方案

Project.Db: Contains my dbContext / Repositories Project.Db:包含我的dbContext /存储库
Project.Core: Contains my POCO classes (used by the dbContext). Project.Core:包含我的POCO类(由dbContext使用)。
Project.Web: My mvc project, which contains the viewModels and POCO-ViewModel mapping classes. Project.Web:我的mvc项目,其中包含viewModels和POCO-ViewModel映射类。

It seems impossible to have a typed projection return an EF entity type. 似乎没有类型化的投影返回EF实体类型是不可能的。 In Project.Db I have the following method. 在Project.Db中,我有以下方法。 I want to get two specific fields (Name and ID) of the regions and their subregions. 我想获取区域及其子区域的两个特定字段(名称和ID)。 I don't want the generated T-SQL to select the unwanted fields. 我不希望生成的T-SQL选择不需要的字段。

public List<Region> GetRegionsAndSubRegions(){
var regions = Context.Regions.Where(r => condition);
var lightRegions = regions.Select(z => new Region{
                                      RegionName = z.RegionName,
                                      RegionId = z.RegionId
                                  }).ToList();
}

This will get me the error 这会让我出错

The entity or complex type 'Region' cannot be constructed in a LINQ to Entities query.

The way I found to get around is this: 我发现解决这个问题的方法是:

var lightRegions = regions.ToList().Select(z => new Region{
                                      RegionName = z.RegionName,
                                      RegionId = z.RegionId
                                  }).ToList();

However, the "ToList()" will select all the fields, which I don't want. 但是,“ ToList()”将选择所有我不需要的字段。

I could project to a ViewModel class, but I don't want that dependency in Project.Db . 我可以投影到ViewModel类,但是我不希望在Project.Db中具有这种依赖关系。 The method should return a list of my POCO entity (List <Region >). 该方法应返回我的POCO实体的列表(列表<Region >)。

All my repository methods return Core (POCO) typed objects to the Web project. 我所有的存储库方法都将Core(POCO)类型的对象返回到Web项目。

Is there a way to do it without projecting to an anonymous type and map it back to Region type (which would be redundant)? 有没有一种方法可以在不投影到匿名类型的情况下将其映射回Region类型(这将是多余的)? Thanks 谢谢

It's possible. 这是可能的。 I just did it with 我只是用
<package id="EntityFramework" version="6.1.3" targetFramework="net461" />

A Vehicle(30+ fields in model & db) is projected to a StatisticsVehicle. 车辆(模型和数据库中的30多个字段)被投影到StatisticsVehicle。

My Query: 我的查询:

var vehicles = 
    _context.Vehicles
        .Select(v => 
            new StatisticsVehicle()
            {
                PlateNumber = v.PlateNumber,
                StatisticsVehicleProcesses = 
                    v.VehicleProcesses.Select(vp => new StatisticsVehicleProcess()
                    {
                        EffectiveIssuanceDate = vp.EffectiveIssuanceDate,
                        PlannedIntakeEndDate = vp.PlannedIntakeEndDate
                    })
            }
        )
        .ToList();

Which results in a sql query only selecting the fields I need from that vehicle and vehicleprocess: 这导致SQL查询仅从该车辆和车辆过程中选择我需要的字段:

SELECT 
    [Project2].[Id] AS [Id], 
    [Project2].[C1] AS [C1], 
    [Project2].[PlateNumber] AS [PlateNumber], 
    [Project2].[C2] AS [C2], 
    [Project2].[EffectiveIssuanceDate] AS [EffectiveIssuanceDate], 
    [Project2].[PlannedIntakeEndDate] AS [PlannedIntakeEndDate]
    FROM ...

It is not possible to directly instantiate an EF Entity type in the projection. 无法在投影中直接实例化EF实体类型。 I think it is to prevent ambiguity like Gert said. 我认为这是为了防止歧义,如格特所说。 The only way to do this is to project to anonymous type and then map to the entity type. 唯一的方法是投影到匿名类型,然后映射到实体类型。

public List<Region> GetRegionsAndSubRegions(){
var regions = Context.Regions.Where(r => condition);
var anonRegions = regions.Select(r => new {
                                  RegionName = r.RegionName,
                                  RegionId = r.RegionId
                              }).ToList();
var lightRegions = anonRegions.Select(r => new Region{
                                  RegionName = r.RegionName,
                                  RegionId = r.RegionId
                              }).ToList();
}

Of course this is a pain in the neck and gets ugly real quick with a lot of table relationships. 当然,这是一个令人头疼的问题,并且由于与许多表之间的关系而变得非常丑陋。

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

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