Getting started with data setup 2

Hi Friends,

This is the continuation of the last section, where i began to setup real time db for our’s movie site. so, let’s get started with the migrations part. However, in the last section we have not configured anything like db connection string, so we didn’t tell explicitly where to make db, what name it should be etc, obviously we can override these things and we can configure these things by using base class constructor as shown below.





so, in the constructor i have said to load the default connection which is being tweaked by me for my convenience. This way i can derive how and where exactly with what name my db will be created. so, with this setting if i build my solution and refresh the page, obviously i’ll loose the data, never mind will push data again.


Now, the next thing which i need to do is use migrations to configure database schemas, seed database and then track for changes in the entity classes. and whenever, these classes will get changed then accordingly database will get updated so that it will keep the database and entity classes in sync. so, the way i’ll be using migrations will be with package manager console window. Basically i launch package manager console window from quick launch section as shown below, and in that window i’ll be typing powershell command to start with the migrations.


47th 48th




so, above is the screen shot of controlling code first migrations. so, basically it gave a skeleton code to get started or to seed values in the db. so, the first thing is AutomaticMigrationsEnabled = False, which means EF won’t make any changes in your db, unless you explicitly instruct EF to do so. But, what i usually do during development, I usually keep it marked as True, because i have been changing my entities so often.



so, what it will basically do, it will go into the database, and see if there is any movie exists with the name, if yes then it will update the same or it will add the same into db.

now, in order to update the db with the above said values, we just need to run the below command in the powershell “Update-database -Verbose”. so, what verbose do basically, it will do force update if there is any data already exists in the db.



so, when i refresh the page, i should be able to see the change with newly seeded values as shown below.



Now, let’s consider a scenario that you are changing the entity and then again you need to update the database, since we have already set AutomaticMigrationsEnabled = true, so don’t need to bother about that, EF will take care about that, so let’s add one more column in the Review class as shown below.

53rd 54th 55th


Now, when i refer the db, it has taken the effect with new value as well. Now, let’s improvise the query a bit as shown below, but before i need to create one View Model, so that i can query against my anonymous type quite significantly in a strongly typed fashion in my view, so ViewModels are again a model which is meant for views. so, suppose you have a complex data model to query against then obviously you could use View Models against that.




Then, when i build and refresh, then page would like some thing like that



Now, in the above query i have added one search criteria to filter the records, now let’s implement that search criteria, so that user can filter records. i have used extension methods in the query so that i can use paging or filtering easily like below:-



so, the above query says that if there is any filtering criteria, then search based on that otherwise return top 10 results based on the average rating.



so, with this i would like to wrap this session. In the next session, we’ll see more about working with data and views together. So, till then stay tuned and happy coding.

Happy Coding