Database-First Approach in Entity Framework and other ORMs

There are two ways of creating ORM models - Model-First approach and Database-First approach. Database-First approach supposes that you already have existing database and build your entity model based on this database. It is used mostly when you have a legacy database and want to

Entity Developer supports both approaches for designing Entity Framework, NHibernate, LinqConnect, Telerik Data Access, and LINQ to SQL models. Here is how you can use Database-First approach in Entity Developer to generate a model and code from an existing database.

Different Ways of Generating Model from Database

Entity Developer offers three ways to generate a model from the database:

Using a convenient wizard

Using a convenient wizard

Just perform a series of wizard steps, specifying the necessary parameters. This is an approach, used in most visual ORM designers. For example, it is used in Visual Studio EDM designer for Entity Framework

Using console commands

Using console commands

Entity Developer has a console version that allows you to generate model from a database and generate code for it using the command line. A similar approach is used for Entity Framework Core with its Scaffold-DbContext command. This way is useful for development process automation.

Dragging tables from Database Explorer

Dragging tables from Database Explorer

Quickly create an empty model via wizard, specify a database connection for it, and then simply drag database tables, views, and stored routines from the Database Explorer window to the diagram. In this way LINQ to SQL models are created.

Entity Developer allows using any of these ways for any of the supported ORMs.

Creating Model via Wizard

Creating a model via wizard is a quite straightforward process.

Creating a Model

First, you need to add a model of the ORM solution of your choice to your Visual Studio project. After you select it, the wizard opens, where you select an approach.
After you select the Database-First approach, you need to configure a connection for it. Select a database provider to use and specify the necessary connection parameters. For ORMs that support third-party providers, Entity Developer supports a number of them.
On the next step, select Generate From Database. After this, select tables, views, and stored routines to add to the model.
After this, configure how names of entity classes and properties are generated from the names of database objects. Entity Developer allows flexible tweaking for the generated names of the entity sets, classes, methods and their members. You may configure case and pluralization, removing and adding of prefixes and suffixes, etc.
Then, you specify the model namespace, context name, and additional setting, affecting the model generation, depending on the ORM selected. For example, for Entity Framework, Entity Developer can automatically detect table-per-type inheritances, many-to-many associations, and more. You can also configure, how entities are added to model diagrams.
Entity Developer offers a flexible T4 template-based code generation with a number of settings and a number of predefined code generation templates for different use cases.

Generating Model from Database Using Console Commands

Entity Developer includes a console application, which can generate a model from a database with a single console command - Create-Model. You just need to specify the required parameters: model file name, provider to use, templates to use (which implicitly specifies the ORM), and the connection string. Here is an example.

ed Create-Model --Provider System.Data.SqlClient 
--Connection "Host=***;UserId=***;Password=***; 
Database=***;" --Templates "EF Core C#" --ModelFile 
"d:\Project\Model.efml" --Tables "Orders", 
"Order Details"

You can also use the --Schemas, --Tables, and --Views parameters to generate model only from the specified database objects. Optionally, you may create a settings file (with another console command), Init-Settings, specify the provider, connection, target framework, and target ORM version in it, and then use this file in the Create-Model command.

After you have the model, you can easily generate code from it with the Generate-Code command and the --ModelFile parameter.

Easy Updating Model from Database

When developing a model using the Database-First approach you may need to make changes to your database and then reflect these changes in your model. With Entity Developer, you can use Update From Database Wizard that allows synchronizing your model with the database for all the supported ORMs in a fast and convenient way.

In Visual Studio, the Update Wizard performs such updating for Entity Framework models. However, when updating model, the wizard resets all manual changes to the model. Unlike standard Visual Studio Update Wizard, Entity Developer Update From Database Wizard tries to preserve manual changes to the model where possible. The wizard detects all the database changes that can affect the model, e.g. created and deleted tables and views, their columns and foreign keys, column datatype changes, created and deleted stored procedures and functions, changes to their parameters etc.

Advanced Model Mapping Tweaking Tools

Entity Developer offers many additional visual tools to configure mapping aspects, that cannot be automatically generated from database. Creating inheritance hierarchies is very easy and can be performed completely without manual editing of XML code. For Entity Framework you may map entities to one or more tables or split a table to two separate entities, create entities (Defining Query) and procedures (Command Text) from SQL statements, use complex types, and edit the storage part of the model.