我是 ASP.NET MVC 4 的新手;使用数据库优先方法。根据我的问题,我正在使用 SQL 存储过程来检索数据以填充我的 jQgrid。但它正在检索除来自 Org 表的 OrgName 之外的所有 BO 表数据(查看存储过程)。那么我该怎么做才能获得 OrgName ???
我的存储过程如下:
ALTER PROCEDURE [dbo].[GetBODetails]
AS
BEGIN
SELECT b.Id, b.OrgId, b.Code, b.Name, o.Name AS OrgName
FROM Org AS o INNER JOIN BO AS b ON o.Id = b.OrgId
END
Columns of BO table :
Id = GUID
Code = NVARCHAR(50)
Name = NVARCHAR(50)
OrgId = Foreign Key ref from Org table Id
Columns of BO table :
Id = GUID
Name = NVARCHAR(50)
通过从模型浏览器窗口执行“添加函数导入”在我的 MVC 项目中使用此过程;通过参考本文单击此处。在执行此操作时,我选择 Returns a collection of Entities is BO 模型。
BO和Org生成的模型如下:
BO.cs
public partial class BO
{
public System.Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public System.Guid OrgId { get; set; }
public virtual Org Org { get; set; }
}
组织.cs
public partial class Org
{
public Org()
{
this.BOes = new HashSet<BO>();
}
public System.Guid Id { get; set; }
public string Name { get; set; }
public virtual ICollection<BO> BOes { get; set; }
}
控制器执行存储过程的代码:
private iregEntities m_oDbCont = new iregEntities();
// GET: /BO/
public JsonResult BOGrid()
{
m_oDbCont.Configuration.ProxyCreationEnabled = false;
var BOList = m_oDbCont.GetBODetails().ToList(); // Executing stored procedure
var JsonBOList = new
{
rows = (
from BOData in BOList
select new
{
id = BOData.Id.ToString(),
cell = new string[] { BOData.OrgId.ToString(), BOData.Code, BOData.Name }
}).ToArray()
};
return Json(JsonBOList, JsonRequestBehavior.AllowGet); returning data into JSON format
}
protected override void Dispose(bool disposing)
{
m_oDbCont.Dispose();
base.Dispose(disposing);
}