Exploring Entity Framework Part 1: A Brief Overview

Introduction A few months ago, I started my new position as a .NET developer.  One of the tasks assigned to me was to rewrite a legacy webforms application from VB.NET to C#.  Seeing as Microsoft is starting to silently shy away from supporting webform apps (it is no longer a development option in .NET Core), it is my opinion that rewriting this application to use an MVC (Model-View-Controller) architecture will help ensure that future developers who may have never worked with webforms will know how to support it.  One of the first challenges I faced was finding a solution that would allow me to configure the model. My solution would require the following:
  1. A means to automatically generate models based on our database architecture.
  2. The flexibility to tweak the auto-generated code so that we can ensure our classes are using the correct data types.
  3. The ability to modify models attributes that do not match what we have in our database.
After researching potential candidates, I came to the conclusion that Entity Framework was the best candidate to satisfy these three requirements.  But what is Entity Framework?   What is Entity Framework Entity Framework is an open-source Object Relational Mapping tool developed by Microsoft that allows developer to treat entities as objects.  It creates a layer of abstraction between the program and database so that the developer can focus more on writing the application and less on how the database works.  Entity Framework gives developers three options to interact with their database: Database First Approach, Code First Approach, and Model First Approach.   Database First Approach The database first approach allows developers to select the tables they wish to interact with in their application.  Once this selection is made, Entity Framework will automatically generate models for each table, and generate an EDMX file that shows each table and all relationships between entities.  The advantage with this approach is that it allows developers to quickly setup their models. This approach works very well if you have an existing database and do not plan to modify its architecture via the application.  Should any changes be made to the database by a DBA, the developer would simply need to refresh the EDMX file so that it pulls those changes into the application’s models. If you wish to make modifications to the model that will not be overwritten after each refresh, you will need to supplement them with their own partial class.   Code First Approach The code first approach gives developers the most control over their models by allowing them to freely modify their attributes.   With code first, you can create a new database for your app using database initializers and migrations. You can even work with an existing database by reverse engineering your models,  which is done selecting the tables you want and having Entity Framework generate the code for you. Unlike the Database First Approach, Code First does not come with an EDMX file.  Additionally, if your database properties are controlled on the database end, you will need to modify your models manually if their corresponding table architecture is changed in the database.  However, if you manage your database through the application, you can use code migrations to ensure that the models in your application are consistent with the entities they represent in your database.     Model First Approach The model first approach allows developers to visually design their entities using with an empty edmx file.  This approach is similar to code first in the way that it takes the models created in the program and creates them in the database as well.  Additionally, the GUI available in the database first approach is used to help design the entities, so the developer can have a graphical representation of each entity and its relationship with others.     Querying Data One of the most appealing features of Entity Framework is that it gives you multiple options to query data.  You can use the SqlQuery function to execute raw SQL commands in a string, or you can use Language Integrated Query (LINQ).  LINQ is a .NET feature that allows the developer to use their preferred .NET programming language (i.e Visual Basic, C#) to query data in place of SQL commands.  This provides many advantages over SQL, such as allowing the developer to use Intellisense to write queries faster, identification of syntax errors before compile-time, identification of type mismatches before executing code, and the ability to use lambda expressions when querying data. When the code is run, Entity Framework converts the LINQ query to a SQL command, which is then fed to the database. If the command includes user input, it is parameterized so that the risk of SQL injection can be lowered. There may be times you wish to use SQL over LINQ.  As mentioned before, you would use the SqlQuery function to execute queries, however your will need to parameterize the query yourself if you wish to help lower the risk of SQL injection attacks.  You would also be without the advantages mentioned above, as your query would be contained in a string and not actual .NET code.   Summary Entity Framework has proved itself to be an excellent solution for creating models in my .NET MVC application.  The three approaches it takes with database interaction (Code First, Database First, Model First) gives developers the flexibility to choose which development method works best for them.  Its compatibility with LINQ also helps developers by giving them the power to write queries in their preferred language’s syntax.   Join me next time when I explore how to use the Code First Approach in Entity Framework.     Have any questions, comments, or concerns about this post?  Notice anything that can be added or improved? If so, please let me know in the comments.  I look forward to hearing from you!