Entity Framework can do a lot for us as Microsoft developers. First of all, it is capable of exposing the database as a set of objects. It does so by utilizing a couple of key classes. First and foremost, you need to be aware of DbContext
. This class is at the heart of Entity Framework Code-First. At a high level, it is a database abstraction. Databases consist of tables, each consisting of rows and columns. DbContext
in turn has generic collection properties; each of which can be typed as DbSet<TRowType>
, corresponding to each table. Each object within the collection, referred to as an entity, represents a row in the corresponding table. Columns are defined by properties of the TRowType
class that is specified as a generic argument of each collection.
Once this structure is laid out, you are capable of querying the underlying database by using LINQ queries. If you add a brand new instance of the TRowType
class to its parent collection and then save the changes using the DbContext
API, this new object will become a row in the corresponding table, where each property value of that object will become a column value in the target row. On top of this, Entity Framework has capabilities to represent other database artifacts, such as procedures and functions. You will be able to query the data using functions, just like tables using LINQ again. The question of evolving the database structure is an important one. In most cases, you will need to add columns and tables, as your application changes. Entity Framework addresses this need via the Migrations feature. This ability will allow you to alter the database structure through C# code. In addition to adding and deleting tables and columns, you will be able to add indexes. Migrations allow developers to evolve a schema without data loss. As you can see, Entity Framework exposes everything you need to access the data in your C# or VB.NET code without wiring SQL and treats your database as another part of your overall application code. You can check migrations code into source control, since it is also C# code!