简体   繁体   English

将EF实体映射到POCO业务实体

[英]Mapping EF entities to POCO business entities

We have to refactor a legacy project, implemented with EF5, VS 2012 and SQL Sevrer 2008 R2. 我们必须重构一个遗留项目,使用EF5,VS 2012和SQL Sevrer 2008 R2实现。
There are different layers: 有不同的层次:

  • BusinessLayers containing POCO classes 包含POCO类的BusinessLayers
  • Data Access layer where the repository pattern is implemented 实现存储库模式的数据访问层
  • Context class (with DbContext) 上下文类(使用DbContext)

At the moment, business POCO classes and EF entities are the same. 目前,业务POCO类和EF实体是相同的。 There is no mapping, but entities generated with the T4 model template are used as business entities as well and business logic is herewith injected with the use of partial classes, as entities extension. 没有映射,但是使用T4模型模板生成的实体也用作业务实体,并且业务逻辑在此注入使用部分类,作为实体扩展。

The goal is to decouple the entities, moving the EF ones into a separate project, that would create a Model layer. 目标是将实体分离,将EF的实体移动到一个单独的项目中,这将创建一个模型层。 My idea would be to create at the business layer business objects that represent the relative counterpart in the Model layer. 我的想法是在业务层创建业务对象,这些对象代表Model层中的相对对应物。

However should I have to manually create the mapping for all the properties between the two layers (in some cases a table can have even 40 fields)? 但是,我是否必须手动为两个层之间的所有属性创建映射(在某些情况下,表可以有40个字段)? Is it possible to use Automapper to achieve this? 是否可以使用Automapper来实现这一目标?

Yes, it is possible to use AutoMapper. 是的,可以使用AutoMapper。 But I suggest you to use Code First approach - persist domain entities directly. 但我建议您使用Code First方法 - 直接保留域实体。 You can write Entity Framework mappings manually or generate them with Reverse Engineer Code First . 您可以手动编写实体框架映射,也可以使用反向工程代码优先生成它们。 That will remove unnecessary complexity from your system. 这将从您的系统中消除不必要的复杂性。

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

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