最近,需要搭建一个新项目,在需求分析时确定数据库中需要创建多个存储过程。所以如果还是用原来EF框架生成ADO.net实体模型的话,不利于修改。

主要是解决以下两个问题:

1、比如前端需要一个值,如果存储过程没有返回,那么在修改存储过程后就得更新实体。很麻烦。

2、前端所需数据类型和返回数据类型不同时直接能映射不需要循环处理。

下面做一个简单的用法介绍(以机场数据为例):

第一个问题:

1、首先用petapoco链接数据库

下载链接:http://pan.baidu.com/s/1dFEfzSd

将文件下载后放在一个文件夹中,然后再这个文件夹中创建一个类文件用于创建与数据链接的桥梁。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using PetaPoco; namespace Entity.Models
{
public class LxaAirportStatistics
{
public static Database Db
{
get
{
if (HttpContext.Current.Items["CurrentDb"] == null)
{
var retval = new Database("AirportEntity");
HttpContext.Current.Items["CurrentDb"] = retval;
return retval;
}
return (Database)HttpContext.Current.Items["CurrentDb"];
}
} }
}

文件中Database的name与web.config中数据库中链接字符串的name一样。

2、现在数据库链接创建好了,就需要创建一个model类用于对数据建立关系。model类字段名称、类型必须和数据库字段名称、类型一致。

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Entity
{
public class Airport
{
public string Airport_DM { get; set; }
public int? Area_DM { get; set; }
public string JC { get; set; }
public string QC { get; set; }
public string TestCol { get; set; }
}
}

  至此,数据库链接和对应关系都创建好了。第一个问题已经解决?有人会问怎么就解决了,那么我可以告诉你,现在数据库的存储过程修改后添加了字段或者删除了字段,只需要在对应的model类对应修改就行,不在需要想使用EF框架一样还得重新生成实体。不信你试试。接下来我们解决第二个问题。

第二个问题:

现在数据虽然从数据库拿出来了,但是前端只需要一部分字段,且类型不同怎么办?

1、创建一个和前端需要的数据类型一样的model类,当然名字的和数据库对应的model类型区别开。

 using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks; namespace IBLL.Dto
{
public class AirportDto
{
public string Airport_DM { get; set; }
public int? Area_DM { get; set; }
public string JC { get; set; }
public string QC { get; set; }
}
}

2、数据模型映射到前端需要的模型,这就需要AutoMapper

下载地址:http://pan.baidu.com/s/1c2Knjfa

引用了dll文件之后就需要配置Automapper

 using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using IBLL.Dto; namespace BLL.AutoMapper
{
public class AutoMapperConfig
{
public static void Configure()
{
Mapper.Initialize(x =>
{
x.CreateMap<double?, string>().ConvertUsing<DoubleToString>();//将数据模型中double?类型全部转化成string
x.CreateMap<Entity.Airport, AirportDto>(); });
} private class DoubleToString : ITypeConverter<double?, string>
{
public string Convert(double? source, string destination, ResolutionContext context)
{
return source == null ? "" : source?.ToString("0.00").TrimEnd('').TrimEnd('.');
}
}
}
}

我这还封装了一下,也一并分享给大家

 using AutoMapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks; namespace BLL.AutoMapper
{
/// <summary>
/// AutoMapper扩展帮助类
/// </summary>
public static class AutoMapperHelper
{
/// <summary>
/// 类型映射
/// </summary>
public static T MapTo<T>(this object obj)
{
if (obj == null) return default(T); return Mapper.Map<T>(obj);
}
/// <summary>
/// 集合列表类型映射
/// </summary>
public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
{ return Mapper.Map<List<TDestination>>(source);
} }
}

3、规则配置完成,现在就剩最后一步。实现对象的完美转换。

 using IBLL;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using BLL.AutoMapper;
using IBLL.Dto;
using Entity.Models; namespace BLL
{
public class BLLAirport : IAirport
{
//获取所有机场
public List<AirportDto> GetALLAirpotsList()
{
var listAirport = AirportEntity.Db.Fetch<Entity.Airport>("select * from Airports);
return listAirport.MapToList<AirportDto>(); }
}
}

至此数据结果已经转换。两个问题也解决。当然项目结构不是完整的,大家应该看得懂的。具体的文件位置可根据自己的项目结构进行调整。

这仅仅是我在项目中遇到时解决的办法,有什么问题或者有其他的办法请大家多多指教。谢谢!

不用EF框架,完成完美实体映射,且便于维护!(AutoMapper,petapoco)的更多相关文章

  1. 释怀我的诺亚尔 不用EF框架,完成完美实体映射,且便于维护!(AutoMapper,petapoco)

    释怀我的诺亚尔   不用EF框架,完成完美实体映射,且便于维护!(AutoMapper,petapoco) 最近,需要搭建一个新项目,在需求分析时确定数据库中需要创建多个存储过程.所以如果还是用原来E ...

  2. 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-entity-mappings-using-fluent-api.a ...

  3. EF Code First:实体映射,数据迁移,重构(1)

    一.前言 经过EF的<第一篇>,我们已经把数据访问层基本搭建起来了,但并没有涉及实体关系.实体关系对于一个数据库系统来说至关重要,而且EF的各个实体之间的联系,实体之间的协作,联合查询等也 ...

  4. EF Code First:实体映射,数据迁移,重构

    经过EF的<第一篇>,我们已经把数据访问层基本搭建起来了,但并没有涉及实体关系.实体关系对于一个数据库系统来说至关重要,而且EF的各个实体之间的联系,实体之间的协作,联合查询等也都依赖于这 ...

  5. EF Code First:实体映射

    二.实体映射 实体与数据库的映射可以通过DataAnnotation与FluentAPI两种方式来进行映射: (一) DataAnnotation DataAnnotation 特性由.NET 3.5 ...

  6. 使用T4模板为EF框架添加数据库实体注释

    网上有一个解决方法如下: http://www.cnblogs.com/stone_w/archive/2012/10/25/2738345.html 不过我试了下没解决太麻烦了 而且一旦EF要重新生 ...

  7. 使用T4模板为EF框架添加数据库实体注释(转)

    1. 下载文件GetSummery.ttinclude2. 把我们下载下来的文件解压,将解压出来的文件放入解决方案中3. 修改下app.config,添加一个连接字符串: <add name=& ...

  8. EF框架step by step(5)—处理实体简单属性

    EF框架会对实体进行跟踪,对实体的每个属性当前值和原始值及其状态进行跟踪,记录.当前值是指实体属性当前的被赋予的值,而原始值是指实体最初从数据库读取或者附加到DbContext时的值. 先通过简单的代 ...

  9. EF框架step by step(7)—Code First DataAnnotations(1)

    Data annotation特性是在.NET 3.5中引进的,给ASP.NET web应用中的类提供了一种添加验证的方式.Code First允许你使用代码来建立实体框架模型,同时允许用Data a ...

随机推荐

  1. Tomcat Server处理一个http请求过程

    假设来自客户端的请求为: http://localhost:8080/lizhx/lizhx_index.jsp 请求被发送到本机端口8080,被在那里侦听的Coyote HTTP/1.1 Conne ...

  2. js中new一个对象的过程

    使用new关键字调用函数(new ClassA(-))的具体步骤: 1. 创建空对象{}: 2. 设置新对象的constructor属性为构造函数的名称,设置新对象的__proto__属性指向构造函数 ...

  3. ES学习笔记

    ES学习 1. 安装 1.1 ES 安装配置 curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5. ...

  4. 在host-only模式下ssh不插网线

    visualbox在host-only模式下,宿主机可以在没有网络的条件下ssh虚拟机. 设置方法: 1.在visualbox中,选择全局设置(preference)--网络(network)-- h ...

  5. 如何在不使用系统函数的情况下实现PHP中数组系统函数的功能

    PHP中为我们提供了各种各样的系统函数来实现我们需要的各种功能,那么,在不使用系统函数的情况下我们要怎样来实现这些功能呢?以下就是几种系统函数的实现方式. 首先,我们来定义一个数组: $arr= ar ...

  6. C语言之字符串

    什么是字符串:使用双引号包含的字符序列. 简单的字符串"hello world" 一个'h'是一个字符 很多个字符组合在一起就是字符串了 字符串与字符数组 在C语言中没有专门的字符 ...

  7. Java IO流--练习

    1)将若干个Student对象,若干个Teacher对象,写出到d:/0404/a.txt中 2)将该文件中所有Student对象反序列化回来装入List, 所有的Teacher对象反序列化回来转入另 ...

  8. Django Nginx反代 获取真实ip

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Andale Mono"; color: #28fe14; backgr ...

  9. (转)导出EXCEL时科学计数法问题

    //1)  文本:vnd.ms-excel.numberformat:@ //2)  日期:vnd.ms-excel.numberformat:yyyy/mm/dd //3)  数字:vnd.ms-e ...

  10. B. Karen and Coffee

    B. Karen and Coffee time limit per test 2.5 seconds memory limit per test 512 megabytes input standa ...