简体   繁体   English

从Entity Framework传递过程结果到视图

[英]Passing result of procedure from Entity Framework to view

The goal 目标

I'm using Entity Framework with C# + ASP.NET MVC 4 and I have a stored procedure in my database (MySQL). 我正在使用带有C#+ ASP.NET MVC 4的实体框架,并且在数据库(MySQL)中有一个存储过程。 I want to display the result of this procedure on my view. 我想在我的视图上显示此过程的结果。

The problem: I don't know the syntax. 问题:我不知道语法。

What I have: on my database context (automatic generated by Entity Framework) there is the following code: 我所拥有的:在我的数据库上下文(由Entity Framework自动生成)上,有以下代码:

public partial class BluMercadosContext : DbContext
{
    public BluMercadosContext()
        : base("name=BluMercadosContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    [...]

    public virtual ObjectResult<getProductsListForHome_Result> getProductsListForHome(Nullable<int> inOffer, Nullable<int> categoryId)
    {
        var inOfferParameter = inOffer.HasValue ?
            new ObjectParameter("inOffer", inOffer) :
            new ObjectParameter("inOffer", typeof(int));

        var categoryIdParameter = categoryId.HasValue ?
            new ObjectParameter("categoryId", categoryId) :
            new ObjectParameter("categoryId", typeof(int));

        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<getProductsListForHome_Result>("getProductsListForHome", inOfferParameter, categoryIdParameter);
    }
}

As you can see, there is a method called getProductsListForHome with two parameters. 如您所见,有一个名为getProductsListForHome的方法,它带有两个参数。 I want to execute this procedure and then display the results on my view. 我要执行此过程,然后在我的视图上显示结果。

And I have to pass the result through controller (MVC) or directly from model to my view (MVVM)? 我必须将结果通过控制器(MVC)传递,还是直接从模型传递到视图(MVVM)?

Thanks in advance. 提前致谢。

The quick & dirty way: 快速而肮脏的方式:

Call the getProductsListForHome method in your controller: 在您的控制器中调用getProductsListForHome方法:

public ActionResult ProductList(SomeType SomeParamIfYouNeed)
{
  using (BluMercadosContext context = new BluMercadosContextEntities())
  {
    ObjectSet<Product> query = context.Products;
    ObjectResult<Product> queryResult = query.Execute(MergeOption.AppendOnly);
  }

  return PartialView("ProductList", queryResult);
}

A better way: 更好的方法:

Call the getProductsListForHome method in a ModelBuilder . ModelBuilder调用getProductsListForHome方法。 That way, your controller stays clean (always remember the rule: a controller method should not exceed 15 lines of code! ) 这样,您的控制器就可以保持干净(始终记住规则: 控制器方法不应超过15行代码!
The following example do use an IOC to bind the IProductBuilder with the right implemetation at runtime (you don't have to do the same, just work with the right implementation directly) 以下示例在运行时确实使用了IOC将IProductBuilder与正确的实现绑定在一起(您不必这样做,只需直接使用正确的实现即可)

ProductController 产品控制器

public ProductController(IProductBuilder productBuilder)
{
  _productBuilder = productBuilder;
}

public ActionResult ProductList(SomeType SomeParamIfYouNeed)
{
  var model = _productBuilder.Get(SomeParamIfYouNeed);

  return PartialView("ProductList", model);
}

ProductBuilder 产品构建器

public List<Product> Get (SomeType SomeParamIfYouNeed)
{
  using (BluMercadosContext context = new BluMercadosContextEntities())
  {
    ObjectSet<Product> query = context.Products;
    ObjectResult<Product> queryResult = query.Execute(MergeOption.AppendOnly);
  }

  return queryResult;
}

An even better way: 更好的方法:

Call the getProductsListForHome method in a ProductService . ProductService调用getProductsListForHome方法。 Would be better for SOC . 对于SOC会更好。 The ProductController will be responsible for returning a model to the view , the ProductBuilder will be in charge of the mapping between the business/domain objects returned by the ProductService and the viewmodel , and finally, the ProductService will be responsible for calling the getProductsListForHome method. ProductController将负责将model返回到viewProductBuilder将负责由ProductService返回的业务/领域对象与viewmodel之间的映射,最后, ProductService将负责调用getProductsListForHome方法。

ProductController 产品控制器

public ProductController(IProductBuilder productBuilder)
{
  _productBuilder = productBuilder;
}

public ActionResult ProductList()
{
  var model = _productBuilder.Get();

  return PartialView("ProductList", model);
}

ProductBuilder 产品构建器

public ProductBuilder(IProductService productService)
{
  _productService = productService;
}

public List<Product> Get ()
{
  // Rather than returning the _productService method call, you could do some mapping between what is returned and the model your view needs.
  return _productService.GetProducts();
}

ProductService 产品服务

public List<Product> GetProducts()
{
  using (BluMercadosContext context = new BluMercadosContextEntities())
  {
    ObjectSet<Product> query = context.Products;
    ObjectResult<Product> queryResult = query.Execute(MergeOption.AppendOnly);
  }

  return queryResult;
}

This code might obviously not compile if you copy-paste it in your project. 如果将其复制粘贴到项目中,则该代码显然可能无法编译。 Its purpose is rather to show you different ways to deal with this kind of situation! 它的目的是向您展示处理这种情况的不同方法!

I think you're looking for DbContext.DataBase.SqlQuery() 我认为您正在寻找 DbContext.DataBase.SqlQuery()

Creates a raw SQL query that will return elements of the given generic type. 创建一个原始SQL查询,该查询将返回给定泛型类型的元素。 The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type. 该类型可以是具有与查询返回的列名相匹配的属性的任何类型,也可以是简单的原始类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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