문제

I am new in ASP.NET MVC 4; Using DATABASE FIRST APPROACH. According to my question I am using SQL stored procedure to retrieve data to populate my jQgrid. But it is retrieving all BO tables data except OrgName which is coming from Org table(look in stored procedure). So What I do to get OrgName???

My Stored Procedure is as follows :

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)

Used this Procedure in my MVC project by doing "Add Function Import" from Model Browser window ;by referring this article Click here. While doing this I select Returns a collection of Entities is BO model.
Model generated of BO and Org are as follows :

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; }
 }   

Org.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; }
 }

Controller's code to execute Stored Procedure :

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);
    }
도움이 되었습니까?

해결책

While doing the function import, ask to create a new complex type instead of mapping the result to the BO entity. If you map to the BO entity, only this entity will be filled up with data, that's why Org is empty.
When mapping to a complex type, EF will automatically create a new class containing all the columns returned by the SP.

Mapping SP result to a specific entity is useful when the SP is returning that particular entity, it avoids creating useless complex types everytime you import an SP.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top