【发布时间】:2011-12-17 20:58:54
【问题描述】:
我正在尝试传递一个映射到名为ListLeagueLeaders() 的方法的存储过程,但调用存储过程的自动生成方法ObjectResult<ListLeagueLeaders_Result> ListLeagueLeaders() 需要一个ObjectResult 类型。我能做些什么来解决这个问题?
我曾想过使用 LINQ 而不是存储过程,但我想给这个策略最后一次机会。相关:LINQ to Entities instead of stored procedure?
HomeController
public class HomeController : Controller
{
HockeyStatsEntities db = new HockeyStatsEntities();
public ActionResult Index()
{
ViewBag.Message = "League leaders";
{
return View(db.ListLeagueLeaders());
}
}
private ICollection<ListLeagueLeaders_Result> ListLeagueLeaders()
{
ICollection<ListLeagueLeaders_Result> leagueLeadersCollection = null;
using (HockeyStatsEntities context = new HockeyStatsEntities())
{
foreach (ListLeagueLeaders_Result leagueLeader in
context.ListLeagueLeaders())
{
leagueLeadersCollection.Add(leagueLeader);
}
}
return leagueLeadersCollection;
}
}
Model1.Context.cs
public partial class HockeyStatsEntities : DbContext
{
public DbSet<Dim_Date> Dim_Date { get; set; }
public DbSet<Dim_Player> Dim_Player { get; set; }
public DbSet<Dim_Team> Dim_Team { get; set; }
public DbSet<Fact_Statistics> Fact_Statistics { get; set; }
public DbSet<Dim_Game> Dim_Game { get; set; }
public virtual ObjectResult<ListLeagueLeaders_Result> ListLeagueLeaders()
{
((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace.LoadFromAssembly(typeof(ListLeagueLeaders_Result).Assembly);
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<ListLeagueLeaders_Result>("ListLeagueLeaders");
}
}
查看:
@model NHLStats2.Models.ListLeagueLeaders_Result
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 entity-framework stored-procedures entity-framework-4