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 自动生成CodeFirst代码_第1张图片

通过点击上图的扩展和更新,得到如下图所示的界面

Entity Framework 自动生成CodeFirst代码_第2张图片

安装完之后只要在项目上右键选择Entity Framework->Reverse Engineer Code First(项目中首先需要安装Entity Framework 包,否则会有错误),然后在弹出的窗口中输入相关的数据库连接信息即可(我们这里使用“NorthWind”数据库)。

Entity Framework 自动生成CodeFirst代码_第3张图片

当然当你在操作的时候你首先还是要先引用Entity Framework。然后点击Reverse Engineer Code First

配置好数据库链接,Entity Framework 自动生成CodeFirst代码_第4张图片

确认之后,我们首先来看一下配置文件的变化

"1.0" encoding="utf-8"?>

  

    

    

"entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  

  

    "NorthwindContext" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True"

      providerName="System.Data.SqlClient" />

  

  

    "v4.0" sku=".NETFramework,Version=v4.5" />

  

  

    "System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">

      

        "v11.0" />

      

    

  

 于此同时生成了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(null);

        }

 

        public NorthwindContext()

            : base("Name=NorthwindContext")

        {

        }

 

        public DbSet Categories { get; set; }

        public DbSet CustomerDemographics { get; set; }

        public DbSet Customers { get; set; }

        public DbSet Employees { get; set; }

        public DbSet Order_Details { get; set; }

        public DbSet Orders { get; set; }

        public DbSet Products { get; set; }

        public DbSet Regions { get; set; }

        public DbSet Shippers { get; set; }

        public DbSet Suppliers { get; set; }

        public DbSet Territories { get; set; }

        public DbSet Alphabetical_list_of_products { get; set; }

        public DbSet Category_Sales_for_1997 { get; set; }

        public DbSet Current_Product_Lists { get; set; }

        public DbSet Customer_and_Suppliers_by_Cities { get; set; }

        public DbSet Invoices { get; set; }

        public DbSet Order_Details_Extendeds { get; set; }

        public DbSet Order_Subtotals { get; set; }

        public DbSet Orders_Qries { get; set; }

        public DbSet Product_Sales_for_1997 { get; set; }

        public DbSet Products_Above_Average_Prices { get; set; }

        public DbSet Products_by_Categories { get; set; }

        public DbSet Sales_by_Categories { get; set; }

        public DbSet Sales_Totals_by_Amounts { get; set; }

        public DbSet Summary_of_Sales_by_Quarters { get; set; }

        public DbSet 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());

        }

    }

}

 最终来查看一下生成的文件

Entity Framework 自动生成CodeFirst代码_第5张图片

代码调用实例

 

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 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代码_第6张图片

简单的使用到此为止。

你可能感兴趣的:(.NET-ORM)