Learn and build web application using MVC and Entity framework code first, code migration approach
Learn MVC and Entity framework code first,code migration approach using sample demo application
- Entity Framework is an Object Relational Mapping (ORM) framework that enables developers to work with relational data has a domain specific object, eliminating the use of most of the data access plumbing code that developers usually need to write.
- Entity Framework is an enhancement to ADO.net that gives developers an automated mechanism for accessing and storing the data.
- Developers can write Linq queries for getting the data from the database using Entity Framework.
- Entity Framework helps us to retrieve and manipulate data as strongly typed objects.
Approaches in Entity Framework
- Model First: database model is created first using the ORM designer tool in Visual Studio. Once the model consisting of entities and relationships has been designed, the physical database and classes will be auto generated from the model with the help of Entity Framework.
- Data Base First: Database is created first or we can use the existing database and then code will be auto generated with the help of Entity Framework.
- Code First: Create a set of classes i.e. code first and the database will be auto generated by the help of Entity Framework.
Why Entity Framework
- There are 2 types of model or groups like
- Logical or Storage Model – deals with tables, primary key, foreign key etc. which will be handled by 1 group of people called as database centric
- Conceptual or Object Oriented Domain Model - deals with objects, properties, inheritance polymorphism etc. which will be handled by 1 group of people called as application centric
- As a result of these 2 models Impedance mismatch occurs and developers devote a lot of time and energy in writing a data access plumbing code like Ado.net for mapping and inserting it to the database
- Ado.net Entity framework seeks to remedy to problem by providing the layer of abstraction between the logical data model and the application domain model
Entity Framework Code First Migration Approach
- Code First is mainly used in Domain Driven Development (DDD) approach. We can focus on the domain design and start writing the classes based on the domain requirements
- Code First API will create a database on the fly based on your entity class and configuration
- Code First Entity framework 4.2 or previous version we need to use any of the database initializers methods and handle to seed or maintain data when there will be a change in the model
- Code First Migration approach is very handy when compared to previous versions, as it is able to track the changes and handle the model changes smartly. Without the need to think about the dropping and recreating data
- Code First Migration allows you to create a new db or update existing db based on your model classes by using package manager console present in visual studio
Advantages of using code first Entity Framework
- Entity Framework enables developers to create data access application by programming against conceptual application model instead of programming against relational data storage schema
- Application can work more in terms of Application centric conceptual model including types like inheritance, complex members and mappings
- Entity Framework is an ORM tool that provides simple API to perform CRUD operations
- Entity Framework also helps us to perform certain DDL and DML operations from the front end, rather than manually doing it in the back end
- Entity Framework provides strongly typed classes, giving
- Intellisense support,
- compile time and
- debugger option
- Entity Framework automatically does the plumbing and mapping for you. So we developers need not to write most of the data access plumbing code i.e. usually required(e.g. Ado.net)
- Lot of time is will be saved and it helps us to quickly develop the application and concentrate more on the Business centric details
MVC and Entity Framework Code First, Code Migration Approach
Create Project and Enable Entity framework code first migration approach
Video : https://www.youtube.com/watch?v=jAyDCUi9H4g
- Create a new MVC 4 web application.
- Give the name of the application and click on OK.
- Select the required template and the View Engine.
- Goto tools -> Library Package Manager -> Package Manger Console.
- Install entity framework - 5 in package manager console by executing the command
- install-package entityFramework -version 126.96.36.199
- Create a new class for DBContext which acts like a bridge or connection object between database and the LINQ object
- DBContext provides a abstraction layer between Application layer and Database layer.
- DBSet will represent the table which will be present in the database.
- Open the web.Config file and provide the required ConnectionString.
- ContextName will be same as the 1 which we created earlier.
- Also give the proper connecion string details like data source, Initial catalog and provider name etc.
- Goto Package Manager Console.
- Enable the migrations for using Code First Code Migrations approach by executing the command
- Enable-Migrations -ContextTypeName DNSContext
- Code-First Migrations will take care of the data. So we no need to write the dropcreate database methods that was used in earlier old version of Entity frame work.
- Once after enabling the migrations for the particular context we are using, we have to Add Migration.
- Add Migration will generate Migration file, sql code and this will not be executed in SQL Server unless and until we run "update-database" command.
- Code Migration File Generated.
- "update-database" command will seed the database by running the code which is present in the code migration file.
- Since we are running it for the first time,new database with name provided in the connection string of initial catalog will be created in the database
- Create a new Course Model with required entities in the file.
- Add a DbSet property in context class
- Add-Migration and update the database
Create CRUD operations using scaffolding template
Video : https://www.youtube.com/watch?v=hwFzUYvbHFo
- Add a Course Controller.
- Select the scaffolding template "MVC controller with read/write actions and views, using Entity Framework".
- Select the Data Context class.
- CRUD operations in the Course Controller will be created with corresponding views will be created using scaffolding templates and Entity framework methods.
- Add a ActionLink in the Nav bar for the Course.
Create a dropdownlist in MVC
- Let us add the dropdown to the domain entity with values hardcoded in the view
- If you want the Hardcoded values to get it from controller
- Provide the ViewBag in the View
Create a Model for Student screen and update database using code Migration
- Create a new model for Student Page.
- Update the DBContext by adding the new property for student
- Now add a new migration for creating Student table in database
- use "Add-Migration" command for creating the migration file
- update the database using "Update-database" command in package manger console
- Create a new Controller for Student with readWrite action selected and add the controller
Create operation in MVC
Video : https://www.youtube.com/watch?v=rGC7OcgkFV0
- Student controller gets created
- Add a View for Create screen
- Run the application and goto "Student/Create" page
- Create an object for DNSContext
- Create a HttpPost method for inserting the data into db.
Render operation in MVC
Video : https://www.youtube.com/watch?v=7y8Cqcpktng
- Retrieve operation in MVC.
- Add a view to display the list of students in the view
- Modify the index page to display all the data in the list
Update operation in MVC
- Edit or update operation in MVC
- post operation in MVC for updating the Student record.
Delete operation in MVC
- Delete operation action method.
- Finally Student page is created with all CRUD operations done.
Configure one To Many and Many to Many relationship in MVC and Entity Framework Code migration approach
Configure Many to Many relationship in MVC and Entity Framework Code migration approach