Data Access Approaches of Entity Framework in .NET

Data Access Approaches of Entity Framework in .NET

Introduction

The intent of this article is to explain the three data access approaches that Microsoft’s Entity Framework provides. There are several good articles on the internet on this topic but I would like to cover this topic in a more detailed way and in the form of a tutorial that would be a primer for someone who is starting to learn Entity Framework and its approaches. We’ll go step by step to explore each approach via which we can access the database and data using EF in our application. I’ll use Entity Framework version 6.2, .NET 4.6, and Visual Studio 2017 for the tutorial. For the database, we would be using SQL Server. You can make use of a local DB if you do not have SQL Server installed. I’ll explain the database first and model first approaches in this article; while the code first approach and code first migrations will be used in the following article.

Entity Framework

Microsoft Entity Framework is an ORM (Object-relational mapping). The definition from Wikipedia is very straightforward for ORM and pretty much self-explanatory.

“Object-relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language. ”

Being an ORM, Entity Framework is a data access framework provided by Microsoft that helps to establish a relation between objects and data structure in the application. It is built over traditional ADO.NET and acts as a wrapper over ADO.NET and is an enhancement over ADO.NET that provides the data access in a more automated way, thereby, reducing a developer’s effort to struggle with connections, data readers, or datasets. It is an abstraction over all those and is more powerful with the offerings it makes. A developer can have more control over what data he needs, in which form and how much. A developer having no database development background can leverage Entity framework along with LINQ capabilities to write an optimized query to perform DB operations. The SQL or DB query execution would be handled by Entity Framework in the background and it will take care of all the transactions and concurrency issues that may occur.

Entity Framework Approaches

It is very common to know the three approaches that Microsoft Entity Framework provides. The three approaches are as follows,

  1. Model First,
  2. Database First, and
  3. Code First
  • Generate database from the data model classes.
  • Generate data model classes from an existing database.

The Model-First approach says that we have a model with all kinds of entities and relations/associations using which we can generate a database that will eventually have entities and properties converted into database tables and the columns and associations and relations would be converted into foreign keys respectively.

The Database-First approach says that we already have an existing database and we need to access that database in our application. We can create an entity data model along with its relationship directly from the database with just a few clicks and start accessing the database from our code. All the entities, i.e., classes, would be generated by EF that could be used in the application’s data access layer to participate in DB operation queries.

The Code-First approach is the recommended approach with EF, especially when you are starting the development of an application from scratch. You can define the POCO classes in advance and their relationships and envision how your database structure and data model may look like by just defining the structure in the code. Entity Framework, at last, will take all the responsibility to generate a database for you for your POCO classes and for the data model and will take care of transactions, history, and migrations.

With all three approaches, you have full control over updating the database and code as per the need at any point in time.

Model First

Using a Model-First approach, a developer may not need to write any code for generating a database. Entity Framework provides the designer tools that could help you make a model and then generate a database out of it. The tools are more of a drag and drop controls that just need inputs like what your entity name is, what properties it should have, how it is related to other entities and so. The user interface is very easy to use and interesting.

Model designer, when good to go, will help you to generate DDL commands that could be directly executed from Visual Studio or on your database server to create a database out of your created model. This creates an EDMX file that stores the information of your conceptual model, storage model, and mapping between both. The only drawback that I can see is that dropping the database completely and recreating it would be a challenge with this approach.

Database First

We use the Database-First approach when we already have an existing database and need to access that in our application. Establishing the data access methodology for existing database with Entity Framework will help us to generate the context and classes in our solution through which we can access the database. It is the opposite of a Model-First approach. Here, a model is created via a database and we have full control to choose what tables to include in the model, and what stored procedures, functions, or views to include. Your application may be a sub-application that does not need all the tables or objects of your big database so you can have liberty here to control what you want in your application and what not. Whenever the database schema changes, you can easily update the entity data model by just one click in the designer or entity data model and that will take care of mapping and create necessary classes in your application.

Code First

Using the Code-First approach, a developer’s focus is only on the code and not on the database or data model. The developer can define classes and their mapping in the code itself and since now Entity Framework supports inheritance, it is easier to define relationships. EF takes care of creating or re-creating the database for you and not only this; while creating a database, you can provide seed data, i.e., the master data that you want your tables to have when the database is created. Using code first, you may not have an EDMX file with relationships and schema as it does not depend upon Entity Framework designer and its tools and would have more control over the database since you are the one who created classes and relationships and managing it.

There is a new concept of code-first migrations which makes the code-first approach easier to use and follow; however, in this article, I’ll not use migrations but old method of creating DB context and DB set classes so that you understand what is under the hood. Code-first approach could also be used to generate code from an existing database, so basically it offers two methods in which it could be used. Read more...

要查看或添加评论,请登录

Akhil Mittal的更多文章

社区洞察

其他会员也浏览了