In today’s discussion, we’ll see more about working with data in case of building Movie Review site. so, the focus of this is to build real time database. so, in this we’ll be working with Entity Framework. so, let’s get started.
so, the 1st step is to get up and running Entity Framework, Entity Framework is the part of.net Framework, and we already have a reference in our project for the same. EF allows me to access relational database using strongly typed c# code. so, when am working with classes, they talk to EF, i don’t have to worry about Sql connections/commands/parameters/data readers.with EF i can work with C# code, and i can issue query using LINQ. There’s few different ways to get started with Entity Framework. They are,
Schema First or Database First:-
so, all of these above mentioned approaches have been exclusively explained in Entity framework category in my blog. you can refer those. In this case, i would be using the last one Code First Approach. so far in the application we were doing all the stuffs with in memory data based out on the below mentioned class.
now, i’ll separate these classes in order to build relationships between the two( Movie and reviews)
so, in the above case i made a separate movie class with all the required parameters in there, apart from that, it also has one ICollection property which holds the collection of all associated Reviews of that movie. now, let me change a bit in the Review Class as well.
so, now my two classes are ready, so these are going to be objects which i would instantiate and save it in database and retrieve the same. Now, let me also add a class to add and persist data, we’ll call it MoviesDb. Now, this class needs to be derived from an entity framework class known as Dbcontext. Now, in the Dbcontext, we can have properties of Dbset that represents the entities that you want to query and persist.
Now, let me show that how easy to use the same in our controller. Let’s use the same in one controller. Now, since it’s a disposable resource, so i should also do some cleanup activity by overriding dispose method simply, when my job is done like shown below
so, now what above line of code will do, that it will go into sql server find where it stored all the movies, retrieve all and put them in the list. Now, with the above code in place i need to change a bit in my view. Now, since this view is already created, so i’ll add a model directive manually since the view is already created, so this is going to be strongly typed against the IEnumerable<Movie>. Now, let’s write the movies out.
Now, let me build the solution, now the thing is for building the same i have commented out in memory database which i have written in the Reviews Controller and when ran the same i got the blank screen,
but i didn’t get any error, which means Entity Framework behind the scenes worked with few database, but where is that database. To answer the question, we need to see the database explorer and add a connection as shown below
so, this was the db which is created by entity framework for me behind the scenes. hence, it didn’t gave me error when i ran the app. However, currently the tables are empty. So, let me go ahead and add few records manually in the db.
so, now when i refresh the page, it will give me that list.
so, with this i would like to wrap this. Now, in the next section, we’ll see more about db migrations and using LINQ queries. Till then stay tuned and happy coding.
1,338 total views, 1 views today