简体   繁体   中英

Using SQL to pull data from multiple tables and display it in one view

I've created my application and using the database first approach. So I used the ADO.net Entity Data Model to add my database to the project and then added controllers to it that way, I don't have any models so to speak I think? But anyway, I've added controllers and CRUD was added to each entity automatically.

My problem is, I want to view data from many tables on one web page. I can do this via sql but how do I use sql to pull the data I need and display it on the screen.

To build a little bit on Dave A's answer:

I personally like doing this type of retrieval with database first EF.

After building the EDMX, create a simple, straight-forward POCO that mimics what you want to return. A simple example would be:

public class ComplexModelFromMultipleTables
{
   List<Car> Cars { get; set; }
   List<Bike> Bikes { get; set; }
   List<Boat> Boats { get; set; }
}

Once you've built the relationships in your database which is reflected in your EDMX, access it in a provider via your favorite pattern. A simple using pattern is always good, though I've built the more complex object with a mapper.

public ComplexModelFromMultipleTables GetObject
using (var db = new DBContext())
{
    var model = new ComplexModelFromMultipleTables
    {
        Cars = db.Cars.Where(x => x.CarType == whateveryouwant).ToList(),
        Bikes = db.Bikes.Where(x => x.anotherproperty == whateveryouwant).ToList(),
        Boats = db.Boats.Where(x => x.something else == whateveryouwant).ToList(),
    }
    return model;
}

Call this provider from your controller and strongly type the view with

@model ComplexModelFromMultipleTables

at the top of your view.

I assume that you are referring to EF Code First approach to creating an Entity Model.

If I'm right, your conflicted by the common practice of using Entities (Table mapped classes) as Models to your Views.

I often use Entities as Models when I first scaffold a page. But As you've found, they are rarely appropriate and I often find myself migrating to more Robust Models.

1) I recommend you create a class library in your Models Directory. Embed make several Entities members of your class.

Forexample, you can have a CustomeActivityModel which also has as members of Customers, Sales, and Orders.

class CustomeActivityModel
{
      Customers CustomerList { get; set; }
      Sales SalesList { get; set; }
      Orders OrdersList { get; set; }
}

within you Controller, you would populate them

   ViewResult Index()
{
   CustomeActivityModel Model = new CustomeActivityModel();
   Model.CustomerList EFContext.Customers;
   Model.SalesList EFContext.Sales;
   Model.OrdersList EFContext.Orders;
   Return View(Model);
}

or alternatively, you can use EF's Linq ability to include Entities that have key relationships (assuming Sales has foreign key for Customers and Orders)

   ViewResult Index()
{
   Model = EFContext.Sales.include("Customers").include("Orders");
   Return View(Model);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM