Visual Studio 2017中从数据库生成VB.NET类的工具及最优方法咨询
Hey there! Let’s dive into the best tools and smarter approaches for generating VB.NET classes that directly mirror your database tables—where each class maps to a table and every property lines up with a column.
Top Tools to Generate VB.NET Classes from Database Tables
1. Visual Studio’s Built-in Entity Framework Reverse Engineering
This is the easiest, no-additional-installation option since it’s baked right into VS2017:
- Right-click your VB.NET project → Add → New Item
- Under the Data category, select ADO.NET Entity Data Model
- Choose Code First from Database (or Database First if you prefer working with an EDMX file)
- Connect to your database, select the tables you want to generate classes for, and click Finish
- VS will auto-generate VB.NET classes for each table, with properties matching column names, data types, and nullability. You’ll also get a
DbContextclass to handle database interactions.
2. Entity Framework Core Scaffolding (Package Manager Console)
If you prefer a more command-line approach with EF Core (lighter weight than EF6), use the Scaffold-DbContext command in the Package Manager Console:
Scaffold-DbContext "YourDatabaseConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Language VB -Force
- Replace
YourDatabaseConnectionStringwith your actual connection string -OutputDir Modelsputs all generated classes in aModelsfolder-Language VBensures output is in VB.NET (default is C#, so this is crucial)-Forcelets you overwrite existing classes if your database schema changes later
3. T4 Text Templates (Customizable Generation)
For full control over the generated code (like adding comments, data annotations, or implementing interfaces), use T4 templates:
- Add a new Text Template (.tt file) to your project
- Write template code to query your database’s schema (via
System.Data.SqlClientor metadata APIs) - Loop through tables and columns to generate VB.NET class code dynamically
- This is great if you need consistent customizations across all your entity classes (e.g., adding
INotifyPropertyChangedfor WPF bindings)
Smarter Mapping Approaches Beyond Basic Generation
Use Data Annotations or Fluent API for Precise Mapping
Auto-generated classes work well out of the box, but you can refine mappings to handle edge cases:
- Data Annotations: Add attributes directly to properties to clarify mappings, e.g.:
Public Class Customer <Key> Public Property CustomerID As Integer <Column("CustomerName")> Public Property Name As String <Required> Public Property Email As String End Class - Fluent API: Configure mappings in your
DbContext’sOnModelCreatingmethod for more complex scenarios (like relationships, composite keys, or custom column types):Protected Overrides Sub OnModelCreating(modelBuilder As ModelBuilder) modelBuilder.Entity(Of Customer)() _ .HasKey(c => c.CustomerID) modelBuilder.Entity(Of Customer)() _ .Property(c => c.Name) _ .HasColumnName("CustomerName") End Sub
Separate Entity Classes from Business Logic
Keep your database-mapped classes clean by separating business logic into separate service classes. This way, your entity classes only handle data storage, and all business rules live elsewhere—making your code easier to maintain and test.
Handle Schema Updates Gracefully
When your database schema changes:
- For EF6: Right-click your EDMX file → Update Model from Database to refresh classes
- For EF Core: Re-run the
Scaffold-DbContextcommand with the-Forceparameter to overwrite existing classes (just make sure you don’t lose custom changes—consider partial classes to add custom code without modifying auto-generated files)
内容的提问来源于stack exchange,提问作者Jonathan Roberts

