简介: 逆向操作DB到Code First Classes
2011-05-30 10:50
232 查看
The Entity Framework team blogged about the EF Power Tools CTP1 today. One of the intriguing features is the ability to reverse engineer an existing database into a set of code first classes.
I put it to a *very* quick test since it’s way past my bedtime.
After installing the tool and creating a new class library project, I reverse engineered the following tiny simple database:
The tool created a bunch of classes grouped into an Entities folder (from tables and views in my db), a set of fluent api mappings grouped into a Mapping folder and a context class. It also added the EntityFramework.dll for EF 4.1 and System.Data.Entity to the project.
A quick look at one of the Customer.cs class:
The Customer mapping file:
and the context:
Very nice!
One of the suggestions in the comments on the team blog post is to be able to select which tables/views get reverse engineered.
原文连接:http://thedatafarm.com/blog/data-access/quick-look-at-reverse-engineer-db-into-code-first-classes/
I put it to a *very* quick test since it’s way past my bedtime.
After installing the tool and creating a new class library project, I reverse engineered the following tiny simple database:
The tool created a bunch of classes grouped into an Entities folder (from tables and views in my db), a set of fluent api mappings grouped into a Mapping folder and a context class. It also added the EntityFramework.dll for EF 4.1 and System.Data.Entity to the project.
A quick look at one of the Customer.cs class:
using System; using System.Collections.Generic; namespace TestEFPowerToolsCTP1.Entities { public class Customer { public Customer() { this.Orders = new List<Order>(); } public int CustomerID { get; set; } public string Title { get; set; } public string FirstName { get; set; } public string MiddleName { get; set; } public string LastName { get; set; } public string Suffix { get; set; } public string CompanyName { get; set; } public string SalesPerson { get; set; } public string EmailAddress { get; set; } public string Phone { get; set; } public System.DateTime ModifiedDate { get; set; } public byte[] TimeStamp { get; set; } public virtual ICollection<Order> Orders { get; set; } } }
The Customer mapping file:
using System; using System.Data.Entity.ModelConfiguration; using System.Data.Common; using System.Data.Entity; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using TestEFPowerToolsCTP1.Entities; namespace TestEFPowerToolsCTP1.Mapping { public class CustomerMap : EntityTypeConfiguration<Customer> { public CustomerMap() { // Primary Key this.HasKey(t => t.CustomerID); // Properties this.Property(t => t.Title) .HasMaxLength(8); this.Property(t => t.FirstName) .IsRequired() .HasMaxLength(50); this.Property(t => t.MiddleName) .HasMaxLength(50); this.Property(t => t.LastName) .IsRequired() .HasMaxLength(50); this.Property(t => t.Suffix) .HasMaxLength(10); this.Property(t => t.CompanyName) .HasMaxLength(128); this.Property(t => t.SalesPerson) .HasMaxLength(256); this.Property(t => t.EmailAddress) .HasMaxLength(50); this.Property(t => t.Phone) .HasMaxLength(25); this.Property(t => t.TimeStamp) .IsRequired() .IsFixedLength() .HasMaxLength(8); // Table & Column Mappings this.ToTable("Customers"); this.Property(t => t.CustomerID).HasColumnName("CustomerID"); this.Property(t => t.Title).HasColumnName("Title"); this.Property(t => t.FirstName).HasColumnName("FirstName"); this.Property(t => t.MiddleName).HasColumnName("MiddleName"); this.Property(t => t.LastName).HasColumnName("LastName"); this.Property(t => t.Suffix).HasColumnName("Suffix"); this.Property(t => t.CompanyName).HasColumnName("CompanyName"); this.Property(t => t.SalesPerson).HasColumnName("SalesPerson"); this.Property(t => t.EmailAddress).HasColumnName("EmailAddress"); this.Property(t => t.Phone).HasColumnName("Phone"); this.Property(t => t.ModifiedDate).HasColumnName("ModifiedDate"); this.Property(t => t.TimeStamp).HasColumnName("TimeStamp"); } } }
and the context:
using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; using TestEFPowerToolsCTP1.Entities; using TestEFPowerToolsCTP1.Mapping; namespace TestEFPowerToolsCTP1 { public class CustomersContext : DbContext { static CustomersContext() { Database.SetInitializer<CustomersContext>(null); } public DbSet<Customer> Customers { get; set; } public DbSet<LineItem> LineItems { get; set; } public DbSet<Order> Orders { get; set; } public DbSet<Product> Products { get; set; } public DbSet<sysdiagram> sysdiagrams { get; set; } public DbSet<custview> custviews { get; set; } public DbSet<vSalesOrderDetail> vSalesOrderDetails { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); modelBuilder.Configurations.Add(new CustomerMap()); modelBuilder.Configurations.Add(new LineItemMap()); modelBuilder.Configurations.Add(new OrderMap()); modelBuilder.Configurations.Add(new ProductMap()); modelBuilder.Configurations.Add(new sysdiagramMap()); modelBuilder.Configurations.Add(new custviewMap()); modelBuilder.Configurations.Add(new vSalesOrderDetailMap()); } } }
Very nice!
One of the suggestions in the comments on the team blog post is to be able to select which tables/views get reverse engineered.
原文连接:http://thedatafarm.com/blog/data-access/quick-look-at-reverse-engineer-db-into-code-first-classes/
相关文章推荐
- Visual Studio 2015使用EF6的CodeFirstFromDB模式操作Sqlite数据库时Provider问题
- EF Codefirst 多对多关系 操作中间表的 增删改查(CRUD)
- Entity Framework Code-First(18):Turn off DB Initializer
- Visual Studio 2015使用EF6的DBFirst模式操作Sqlite数据库
- [C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例
- 【EF6学习笔记】(一)Code First 方式生成数据库及初始化数据库实际操作
- EF下CodeFirst、DBFirst与ModelFirst分析
- 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解
- EF Codefirst 多对多关系 操作中间表的 增删改查(CRUD)
- EF Codefirst方式数据库维护操作
- Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例
- Code First for MySql命令操作
- 8天掌握EF的Code First开发系列之2 简单的CRUD操作
- Code First Entity Framework基本与最佳添加(add/create),删除(delete/remove),修改(update/modify)操作
- Model backing a DB Context has changed; Consider Code First Migrations
- asp.net MVC code first Migrations : Model 同步到DB中
- 查找删除Code First Entity Framework基本与最佳添加(add/create),删除(delete/remove),修改(update/modify)操作
- Entity Framework Code-First(14):From Existing DB
- [C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例
- Code First开发系列之管理数据库创建,填充种子数据以及LINQ操作详解