Code first We will convert database operations to tables or tables on the database side by modeling only the classes we have written in the visual studio environment. Entity Framework is one of the modeling types Code First The main purpose of the approach is to use the model classes, which we have created quickly and effectively, as tables (datasets) in our project and carry out database operations.
To add these class models to the database that will serve as the table we wrote with Visual Studio, or to make the database aware of any changes made to these classes on the code side. Migration We’ll use.
In the Code First approach, let’s take certain steps together with an example. So I think you can understand more.
Things to do
- Install the Entity Framework from the NuGet package installer and let’s include it as a package.
- Let’s create classes like a table.
public class Book
public int BookId get; set;
public string Name get; set;
public Detail Detail get; set;
public int CategoryId get; set;
public virtual Category Category get; set;
public class Detail
public string Author get; set;
public int PageSize get; set;
public class Category
public int CategoryID get; set;
public string Name get; set;
public string Description get; set;
public ICollection Book get; set;
- Let’s configure the mapping of each of the classes that will serve as a table using Fluent Api.
The configuration file (CategoryMaps.cs) I created for the Category Class using fluent api
The configuration file I created for the Book Class using fluent api (BookMaps.cs)
- dbcontext We will create a context class that will connect to the database that we can use from the class and perform database operations. (In our project, I will call this class Db)
- dbcontext our class dbset Let’s mark the classes we created with.
- onmodelcreating In the method we use the class DBModelBuilder from a class called modelBuilder to create the model as in the picture.
- In our context class called Db, config Let’s create a Connection String and specify the connection name.
Data Source-> your database server name.
Initial Catalog_> your database name (whatever database you create will create a database with that name.)
- Tools menu Click NuGet Package Manager-> Package Manager Console to open the console screen. The Add-Migrations command is issued here. After the command runs, the configration.cs file is created in the Migrations folder in our project. Update-database erbVerbose command now creates the database. If there is a change in your classes, make the change and give the Update – database –verbose command from PM (package manager console) as follows.
We check our database created through Sql Server Management Studio. As shown in the picture below, a migrationHistory table has been created. The other picture shows the diagram showing the relationship between the tables.
We can now create a controller and access our category class (table) using our context class Db and perform various actions. Make a Controller named Default and create Index and Create action as follows.