Entity Framework 自动生成CodeFirst代码
前言
在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发。今天就让我们一起看一下使用Entity Framework Power Tools如何基于现有数据库生成数据类和数据库上下等。
Entity Framework Power Tools
基于现有数据库生成POCO数据类和数据库上下文需要借助Visual Studio一个扩展插件-- Entity Framework Power Tools(一个Code First反向工程工具)。
通过点击上图的扩展和更新,得到如下图所示的界面
安装完之后只要在项目上右键选择Entity Framework->Reverse Engineer Code First(项目中首先需要安装Entity Framework 包,否则会有错误),然后在弹出的窗口中输入相关的数据库连接信息即可(我们这里使用“NorthWind”数据库)。
当然当你在操作的时候你首先还是要先引用Entity Framework。然后点击Reverse Engineer Code First
配置好数据库链接,
确认之后,我们首先来看一下配置文件的变化
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="NorthwindContext" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
于此同时生成了NorthWindContext数据库操作上下文
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using CodeFirstPowerTools.Models.Mapping; namespace CodeFirstPowerTools.Models
{
public partial class NorthwindContext : DbContext
{
static NorthwindContext()
{
Database.SetInitializer<NorthwindContext>(null);
} public NorthwindContext()
: base("Name=NorthwindContext")
{
} public DbSet<Category> Categories { get; set; }
public DbSet<CustomerDemographic> CustomerDemographics { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<Order_Detail> Order_Details { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Region> Regions { get; set; }
public DbSet<Shipper> Shippers { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<Territory> Territories { get; set; }
public DbSet<Alphabetical_list_of_product> Alphabetical_list_of_products { get; set; }
public DbSet<Category_Sales_for_1997> Category_Sales_for_1997 { get; set; }
public DbSet<Current_Product_List> Current_Product_Lists { get; set; }
public DbSet<Customer_and_Suppliers_by_City> Customer_and_Suppliers_by_Cities { get; set; }
public DbSet<Invoice> Invoices { get; set; }
public DbSet<Order_Details_Extended> Order_Details_Extendeds { get; set; }
public DbSet<Order_Subtotal> Order_Subtotals { get; set; }
public DbSet<Orders_Qry> Orders_Qries { get; set; }
public DbSet<Product_Sales_for_1997> Product_Sales_for_1997 { get; set; }
public DbSet<Products_Above_Average_Price> Products_Above_Average_Prices { get; set; }
public DbSet<Products_by_Category> Products_by_Categories { get; set; }
public DbSet<Sales_by_Category> Sales_by_Categories { get; set; }
public DbSet<Sales_Totals_by_Amount> Sales_Totals_by_Amounts { get; set; }
public DbSet<Summary_of_Sales_by_Quarter> Summary_of_Sales_by_Quarters { get; set; }
public DbSet<Summary_of_Sales_by_Year> Summary_of_Sales_by_Years { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CategoryMap());
modelBuilder.Configurations.Add(new CustomerDemographicMap());
modelBuilder.Configurations.Add(new CustomerMap());
modelBuilder.Configurations.Add(new EmployeeMap());
modelBuilder.Configurations.Add(new Order_DetailMap());
modelBuilder.Configurations.Add(new OrderMap());
modelBuilder.Configurations.Add(new ProductMap());
modelBuilder.Configurations.Add(new RegionMap());
modelBuilder.Configurations.Add(new ShipperMap());
modelBuilder.Configurations.Add(new SupplierMap());
modelBuilder.Configurations.Add(new TerritoryMap());
modelBuilder.Configurations.Add(new Alphabetical_list_of_productMap());
modelBuilder.Configurations.Add(new Category_Sales_for_1997Map());
modelBuilder.Configurations.Add(new Current_Product_ListMap());
modelBuilder.Configurations.Add(new Customer_and_Suppliers_by_CityMap());
modelBuilder.Configurations.Add(new InvoiceMap());
modelBuilder.Configurations.Add(new Order_Details_ExtendedMap());
modelBuilder.Configurations.Add(new Order_SubtotalMap());
modelBuilder.Configurations.Add(new Orders_QryMap());
modelBuilder.Configurations.Add(new Product_Sales_for_1997Map());
modelBuilder.Configurations.Add(new Products_Above_Average_PriceMap());
modelBuilder.Configurations.Add(new Products_by_CategoryMap());
modelBuilder.Configurations.Add(new Sales_by_CategoryMap());
modelBuilder.Configurations.Add(new Sales_Totals_by_AmountMap());
modelBuilder.Configurations.Add(new Summary_of_Sales_by_QuarterMap());
modelBuilder.Configurations.Add(new Summary_of_Sales_by_YearMap());
}
}
}
最终来查看一下生成的文件
代码调用实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeFirstPowerTools.Models; namespace CodeFirstPowerTools
{
class Program
{
static void Main(string[] args)
{
using (var db = new NorthwindContext())
{
IQueryable<Order> orders = from order in db.Orders
where order.OrderID < 10256
select order;
foreach (Order order in orders)
{
Console.WriteLine(order.ShipCity);
}
}
Console.ReadLine();
}
}
}
执行后结果如下
简单的使用到此为止。
Entity Framework 自动生成CodeFirst代码的更多相关文章
- MetadataType来帮助entity framework自动生成的代码进行标注
真的是,用的时候就四处google,还是记在这里容易找 [MetadataType(typeof(Person.Metadata))] public partial class Person { pr ...
- Visual Studio Entity Framework (EF) 生成SQL 代码 性能查询
Visual Studio Entity Framework (EF) 生成SQL 代码 性能查询 SQL 中,有SQL Server Profiler可以用来查询性能以及查看外部调用的SQL ...
- Entity Framework 自动生成代码 如何用继承
分部类 用接口
- Oracle中使用Entity Framework 6.x Code-First
Oracle中使用Entity Framework 6.x Code-First方式开发 去年写过一篇EF的简单学习笔记,当时EF还不支持Oracle的Code-First开发模式,今天无意又看了下O ...
- 关于Entity Framework自动关联查询与自动关联更新导航属性对应的实体注意事项说明
一.首先了解下Entity Framework 自动关联查询: Entity Framework 自动关联查询,有三种方法:Lazy Loading(延迟加载),Eager Loading(预先加载) ...
- [Dynamic Language] 用Sphinx自动生成python代码注释文档
用Sphinx自动生成python代码注释文档 pip install -U sphinx 安装好了之后,对Python代码的文档,一般使用sphinx-apidoc来自动生成:查看帮助mac-abe ...
- wsdl自动生成Java代码,根据wsdl生成Java代码
wsdl自动生成Java代码,根据wsdl生成Java代码 >>>>>>>>>>>>>>>>>&g ...
- 使用xorm工具,根据数据库自动生成 go 代码
使用xorm工具,根据数据库自动生成 go 代码 引入 使用 golang 操作数据库的同学都会遇到一个问题 -- 根据数据表结构创建对应的 struct 模型.因为 golang 的使用首字母控制可 ...
- mybatis自动生成java代码
SSM框架没有DB+Record模式,写起来特别费劲,只能用下面的方法勉强凑合. 上图中,*.jar为下载的,src为新建的空白目录,.xml配置如下. <?xml version=" ...
随机推荐
- poj2154-color-polyan次二面体+欧拉函数优化
N<=1e9,O(nlogn)的做法会超时.从枚举置换转变为枚举轮换长度,然后可以利用欧拉函数,把复杂度变为O(√n * logn) /*---------------------------- ...
- Promise 学习笔记 - 时间支配者
本文同步自我的个人博客:http://www.52cik.com/2015/11/08/promise.html JavaScript 的 promises 事实标准称为 Promises/A+.ES ...
- WPF开发时光之痕日记本(一)——富文本编辑器
本篇给大家推荐一个 WPF 版的富文本编辑器,SmithHtmlEditor,具体网址大家可以找一找,我在这个编辑器的基础上修改了界面,增加了一些功能,模仿了kindeditor 的界面,鉴于自己现在 ...
- Koala Framework是什么?我为什么要写这个框架?
当时的监管组,技术力量累积的很少,还在直连DB,使用着DataTable.DataSet作为数据的承载,监管是公司最近几年主推的项目,所以领导们决定进行重要调整. 初来乍到 由于之前没有任何的技术积累 ...
- 初探JAVA中I/O流(一)
一.流 流,这里是对数据交换的形象称法.进程是运行在内存中的,在运行的过程中避免不了会与外界进行数据交互.比如将数据从硬盘.控制台.管道甚至是套接字(具体点应该是我们电脑上的网卡)读到我们进程锁所占据 ...
- jQuery使用之(三)处理页面的元素
对于页面的元素,在DOM编程中可以通过各种查询.修改手段进行管理,非常麻烦.jQuery提供了一整套的方法来处理页面的元素.包括元素的内容.复制.移动和替换等.本节将介绍一些常用的内容. 1.直接获取 ...
- 无法加载协定为“ServiceReference1.xxxxx”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分
原因是config节点中有多个endpoint相同节点,提示按名称指示首选的终结点,说明程序不知道选那个节点. 解决办法,实例化service服务对象时,通过name值指定创建它. config文件部 ...
- 微信WeixinJSBridge API
<!DOCTYPE html><html> <head> <title>微信WeixinJSBridge API</title> <m ...
- Nginx下用webbench进行压力测试
在运维工作中,压力测试是一项非常重要的工作.比如在一个网站上线之前,能承受多大访问量.在大访问量情况下性能怎样,这些数据指标好坏将会直接影响用户体验. 但是,在压力测试中存在一个共性,那就是压力测试的 ...
- myeclipse自动import
不管包什么的 直接把代码全写出来 再按 ctrl + shift +o 这是自动导包的 前提是你写的代码是正确的