Introduction
In this article, we will learn how you can use multiple table bases by selecting View in MVC 5.0. Select View is basically a select query , which selects your data from your database.
Now, let's start.
Step 1
Here, we will create our database demo2 and two tables (student,college).
Step 2
Now, we will create our select view, which selects the data from both the tables.
- CREATE VIEW [dbo].[stud_detaile] AS SELECT id,name,rollno,classs,subjects,fees,collegename from student join college on student.collegeId=college.collegeId;
Step 3
Now, we will open Soluction Explorer and create a Home controller. After that select-> Model and Right click and select Add. Afterwards, select New item.
Afterwards, select ADO.NET Entity Data Model and click Add button.
Afterwards, an Entity Data Model Wizard opens and select Generate from database option. Subsequenty, click Next option. Here, before creating new connection, click Next option and choose an Entity Framework 5.0. Click Next and select your Views, followed by stud_detaile and click Finish button.
In this way, we have created our model.
Now, we need to double click on Model1.edmx.
Now, three classes are automatically created. The first two classes represents table and third class stud_detail represents our select view.
Step 4
In this step, open Home controller. Give the reference of Model and create the object of connection class.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Webtest1.Models;
- namespace Webtest1.Controllers {
- public class HomeController: Controller {
- demo2Entities1 ds = new demo2Entities1();
- public ActionResult Index() {
- return View();
- }
- }
- }
- Now we are create show_detaile() method.
- [HttpGet]
- public ActionResult show_detail1() {
- var data = ds.stud_detaile.ToList();
- return View(data);
- }
Add new view.
Now, your view is ready.
Now, build the solution and run it. Afterwards, your output is ready.