Entity Framework Database First Discover How to Use this Approach
What is Entity Framework Database First?
When you start learning Entity Framework, you will also often see the term Entity Framework Database First. Database first is one of the three approaches to create an entity model.
Answer
In the Entity Framework, the Database First Approach provides an alternative to the Code First by creating POCO classes from the existing database.
- Database first approach is used when a database is ready; then Entity Framework will complete its duty and create POCO entities for you
- If you already had a designed database and you don't want to do extra efforts, then you can go with this approach.
- You can modify the database manually and update the model from a database.
- So, we can say, entity framework can create your model classes based on tables and columns from the relational database.
Create Model Using Database First Approach
In the Code First, we have created a database from a DbContext and classes.
- It is also possible to reverse engineer an existing database into a DbContext and classes, and it is known as Database First approach.
- We have a simple database created which contains
Books
table.
Let's create a new empty project and add the EntityFramework NuGet package which is explained in the Code First approach.
We will use of Entity Framework Designer, which is included as part of Visual Studio, to create our model.
Right click on the project in Solution Explorer and choose Add > New Item...
option.
Select Data from the left menu and then ADO.NET Entity Data Model
.
Enter BookStore as the name and click Add. This launches the Entity Data Model Wizard. Select EF Designer from Database and click Next.
Select the connection to the database you created in the Code First, enter BookContext as the name of the connection string and click Next.
Click the checkbox next to Tables you want to import and click Finish.
Now let's look at the new DbContext class.
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace DatabaseFirst { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class BookContext : DbContext { public BookContext() : base("name=BookContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet<Book> Books { get; set; } } }
You can see that DbSet property is created Book
Entity class. You can now use a LINQ query to read/write from/to the database.
using (var db = new BookContext()) { db.Books.Add(new Book { Title = "Introduction to Programming", Date = DateTime.Now}); db.SaveChanges(); foreach (var book in db.Books) { Console.WriteLine(book.Title); } }
Update Model From Database
You can also update your model when the Database changes. Let's add AuthorName
column to the database.
To update model from the database, right-click the .edmx file and select Update Model from Database.
Expand the Tables, Views, and Stored Procedures nodes, and check the objects you want to add to the .edmx file.
Click Finish to update the .edmx file with the database changes.
You can see that AuthorName
property is added to the model, now we need to update the Book
class as well.
To update the table field changes in the BookStore.tt file, right on BookStore.tt file and choose Run Custom Tool.
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace DatabaseFirst { using System; using System.Collections.Generic; public partial class Book { public int BookId { get; set; } public string Title { get; set; } public System.DateTime Date { get; set; } public string Publisher { get; set; } public string AuthorName { get; set; } } }
Now you can see that the Book
class is also updated.
Related links
For further details, please see the following links.
- https://www.tutorialspoint.com/entity_framework/entity_database_first_approach.htm
- https://www.entityframeworktutorial.net/entityframework6/introduction.aspx
ZZZ Projects