Introducing Entity Framework Core

 






Creating A Data Project

To preserve our blog entries, we can utilize Entity Framework, which is Microsoft's Object Relational Mapping (ORM) tool. This framework allows developers to interact with data through domain-specific classes without needing to concern themselves with the underlying database.

There are two ways to use Entity Framework:

  • The database-first approach: This is when we already have an existing database and generate classes based on that database.

  • The code-first approach: This is when we first write the classes, which will then generate the database.

Creating A New Project

There are numerous methods for storing data. We can implement an SQLite database when developing the blog. The information will be available for both the Blazor WebAssembly project and the Blazor Server project.

To create a new project, follow these steps:

  • Open a PowerShell prompt.

  • Navigate to the MyBlog folder.

  • Create a class library (classlib) by typing the following command-

dotnet new classlib -o MyBlog.Data

The dotnet tool should now have created a folder called MyBlog.Data.

  • Add the new project to our solution by running the following command-

dotnet sln add MyBlog.Data

It will look for any solution in the current folder. If for any reason we already have a solution, we need to specify that as well.

Adding NuGet Packages

To be able to use Entity Framework Core, we need to add a couple of NuGet packages to our project:

  • Open PowerShell and navigate to the MyBlog.Data folder- cd MyBlog.Data

  • Add the Microsoft.EntityFrameworkCore.Tools package to the project using the following command- dotnet add package Microsoft.EntityFrameworkCore.Tools

  • Add the Microsoft.EntityFrameworkCore.Sqlite package to the project with the following command- dotnet add package Microsoft.EntityFrameworkCore.Sqlite

Creating Data Classes

Now we need to create a class for our blog post. To do that we will go back to Visual Studio:

  • Open the MyBlog solution in Visual Studio (if it is not already open). We should now have a new project called MyBlog.Data in our solution. We might get a popup asking if we want to reload the solution. click Reload if so.

  • Right-click on the MyBlog.Data project and select Add | New Folder. Name the folder Interfaces.

  • Next, we need to create an interface, just so that we don't have to repeat the code later on. Right-click in the Interfaces folder and select Add | Class. In the list of different templates, select Interface and name it IMyBlogItem.cs.

  • Open IMyBlogItem.cs and replace its content with a code.

  • Now we need to create three data classes. Right-click on MyBlog.Data and select Add | New Folder. Name the folder Models.

  • Right-click on the Models folder and select Add | Class. Name the class BlogPost.cs and press Add.

  • Right-click on the Models folder and select Add | Class. Name the class Tag.cs and press Add.

  • Open BlogPost.cs and replace the content with a code.

  • Open Category.cs and replace the content with a code.

  • Open Tag.cs and replace the content with a code.

Now we have created a couple of classes that we will use.

Conclusion

Some steps to create a database project using Entity Framework are discussed above.































Comments

Popular posts from this blog

Deployment (Part 3)

Deployment (Part 1)

Deployment (Part 2)