‫What we're going to take a look at now is a brief introduction to entity framework, something that
‫we're going to be using significantly in the development of our application.
‫And we're going to take a look at this introduction just to give you a sense of where it fits in our
‫application and what we use it for.
‫So the first question is, well, what is it?
‫Well, it's an object relational mapper and its job is to translate our code into SQL commands that
‫update our tables in the database.
‫Now prior to net 3.5, which was released a long time ago in 2002, not core 3.5, we're talking about
‫the original dot net framework.
‫We often used to write something called Adobe Code to save or retrieve application data from an underlying
‫database.
‫We would have to open a connection to the database, create a data set to fetch or submit the data,
‫then convert the data from the data set to net objects.
‫And it was a cumbersome and error prone process because we would be writing that database access code
‫as SQL code, which would be in the form of strings, and it's very easy to make mistakes.
‫So Microsoft has provided a framework called Entity Framework to automate all these database related
‫activities for our application.
‫So what we have at the moment is an app user entity, just a simple class that we've created with an
‫ID property that's an integer and a string property for the user name.
‫What we're going to introduce is entity framework.
‫And when we add entity framework, we need to create an important class that derives from the DB context
‫class that we get with entity framework.
‫Now this class acts as a bridge between our domain or entity classes and the database.
‫And this DB context class is the primary class we use for interacting with our database.
‫Now what entity framework does for us if we just take a look at an example, is it allows us to write
‫something called link queries, which is what we see here on the left.
‫We say that we get our DB context users because when we add our user entity to our DB context class,
‫then we'll have access to a property called users, which represents our users table in our database.
‫And then for example, we could add a new user with an ID of four and a name equal to John.
‫Now Entity Framework works with database providers.
‫Now the one that we're going to use purely for development is SQL, Lite's SQL.
‫It means we don't need to install a database server and SQL Lite just uses a file to store our database.
‫Now, SQL LTS is not production worthy, but it is a great database for development because it's very
‫portable.
‫We don't need to install anything and it simply adds a file into our project's folder for the database.
‫Later on, we will take a look at changing lists to a production where the database but for the development,
‫we're just going to stick with SQL.
‫LTS Now I know some of you Windows guys are going to be saying, Can't we just use SQL Server?
‫Yes, of course you can.
‫But for development, I'm going to stick with SQL Lite because SQL Server is not cross-platform, whereas
‫SQL Lite is, it will work on any operating system.
‫So our SQL provider or our database provider is responsible for translating our link query on the left
‫into a SQL Command on the right.
‫And it will translate our code on the left into an insert into users with the ID name and that it'll
‫insert the values inside there and we'll add that particular user to our database.
‫So just a couple of features to go through.
‫Entity framework allows us to query our database using linked queries that I mentioned very briefly
‫before.
‫It also gives us change tracking.
‫It will keep track of entity framework.
‫We'll keep track of changes occurring in our entities which need to be submitted to the database.
‫It also allows us to save the database and entity framework will execute, insert, update and delete
‫commands to the database as the DB context class gives us a save changes method that we can use.
‫We'll see that very soon.
‫It also gives us concurrency.
‫It uses optimistic concurrency by default to protect overwriting changes made by another user.
‫Since data was fetched from the database, it also deals with transactions and it provides automatic
‫transaction management whilst querying.
‫Or saving data.
‫It also includes first level caching out of the box, so repeated querying will return data from the
‫cache instead of hitting the database.
‫It also offers built in conventions and when I was talking about why the ID property needs to be named
‫ID is because entity framework follows conventions and includes a set of default rules which automatically
‫configure the entity framework schema or the model that we use to create our database.
‫We can also configure our entities and there's ways to configure these so that we can override the conventions
‫if we wish to do so.
‫And it also offers us migrations and we'll take a look at this soon.
‫But this gives us an ability to create a database schema so that when we start our application or we
‫run a particular command, we can automatically generate our database in our database server.
‫Now these are pretty powerful and they're very convenient because they take a look at our code that
‫we write and then creates the appropriate database that we need, or the database schema that we need
‫to manage our database.
‫So we don't need to create our database manually.
‫Entity framework can do this for us.
‫There is another way around.
‫This is referred to as code first database creation.
‫You can do it the other way around.
‫You can create a database first if you want, and then go ahead and create your entities to match up
‫with the database.
‫But it's far more convenient to use migrations to do so, and all of these features will be taking a
‫look at coming up.