2016-08-12 210 views
0

我是ASP.NET的新手,創建了接受ID數組作爲輸入參數並查詢Oracle DB的MVC框架Web API,這應該以JSON格式返回結果。我們的查詢是像在Web API中實現實體框架

SELECT STCD_PRIO_CATEGORY_DESCR.DESCR AS CATEGORY, 
     STCD_PRIO_CATEGORY_DESCR.SESSION_NUM AS SESSION_NUMBER, 
     Trunc(STCD_PRIO_CATEGORY_DESCR.START_DATE) AS SESSION_START_DATE 
     from STCD_PRIO_CATEGORY 
     where STCD_PRIO_CATEGORY_DESCR.STD_REF IN(X,Y,Z) 

其中X,Y,Z是我們將通過爲輸入值的參數

我創建的API控制器

public class PDataController : ApiController 
    { 
    public HttpResponseMessage Getdetails([FromUri] string[] id) 
    { 
    List<OracleParameter> prms = new List<OracleParameter>(); 
    string connStr = ConfigurationManager.ConnectionStrings["PDataConnection"].ConnectionString; 
    using (OracleConnection dbconn = new OracleConnection(connStr)) 
    { 
    var inconditions = id.Distinct().ToArray(); 
    var srtcon = string.Join(",", inconditions); 
    DataSet userDataset = new DataSet(); 
    var strQuery = @"SELECT STCD_PRIO_CATEGORY_DESCR.DESCR AS CATEGORY, 
     STCD_PRIO_CATEGORY_DESCR.SESSION_NUM AS SESSION_NUMBER, 
     Trunc(STCD_PRIO_CATEGORY_DESCR.START_DATE) AS SESSION_START_DATE   
     from STCD_PRIO_CATEGORY 
     where STCD_PRIO_CATEGORY_DESCR.STD_REF IN("; 
    StringBuilder sb = new StringBuilder(strQuery); 
    for(int x = 0; x < inconditions.Length; x++) 
     { 
      sb.Append(":p" + x + ","); 
      OracleParameter p = new OracleParameter(":p" + x,OracleDbType.NVarchar2); 
      p.Value = inconditions[x]; 
      prms.Add(p); 
     } 
    if(sb.Length > 0) sb.Length--; 
    strQuery = sb.ToString() + ")"; 
    using (OracleCommand selectCommand = new OracleCommand(strQuery, dbconn)) 
     { 
     selectCommand.Parameters.AddRange(prms.ToArray()); 
     using (OracleDataAdapter adapter = new OracleDataAdapter(selectCommand)) 
     { 
      DataTable selectResults = new DataTable(); 
      adapter.Fill(selectResults); 
      var returnObject = new { data = selectResults }; 
      var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json")); 
      ContentDispositionHeaderValue contentDisposition = null; 
      if (ContentDispositionHeaderValue.TryParse("inline; filename=ProvantisStudyData.json", out contentDisposition)) 
      { 
       response.Content.Headers.ContentDisposition = contentDisposition; 
      } 
      return response; 
}}}}}} 

它完美並返回結果爲 {"data":[{"CATEGORY":"Internal Study","SESSION_NUMBER":7,"SESSION_START_DATE":"2015-02-13T00:00:00"}]}

不過想利用該模型在這裏實現實體框架和DBContext.I創建的模型類和DataContext類如下

namespace PSData.Models 
{ public class StudyDataModel 
{ [Key] 
public string CATEGORY { get; set; } 
public int SESSION_NUMBER { get; set; } 
public DateTime SESSION_START_DATE { get; set; } 
}} 

而且

namespace PSData.Models 
{ 
public class StudyDataContext:DbContext 
{ 
public DbSet<StudyDataModel> details { get; set; } 
}} 

我不知道如何在控制器中實現它們。當我試圖創建一個使用Web API 2 Controller with actions,using Entity Framework同時選擇模型類,它創造控制器

private StudyDataContext db = new StudyDataContext(); 
// GET: api/StdData 
public IQueryable<StudyDataModel> Getdetails() 

數據庫上下文類我不知道如何着手爲返回類型是我在其他控制器的HttpResponseMessage控制器在哪裏我正在返回JSON消息。任何幫助大大增益​​

+0

您需要創建'StudyDataContext'對象,然後才能訪問details屬性。我建議你在構建大系統之前花費至少30分鐘的時間完成EF基礎教程。 – Shyju

+0

@Shyju是我創建的對象作爲'私人StudyDataContext db = new StudyDataContext()'所以我明白,如果我使用'返回db.details;'它返回模型類中的所有鍵。但不知道如何將它們轉換爲JSON返回 – trx

回答

1

你不需要明確地將其轉換爲json格式。 content negotiation模塊和媒體格式化程序將負責根據請求將數據轉換爲所需的格式(XML/JSON)。默認情況下,它返回JSON。

假設你有一個DTO類這樣

public class CategoryDto 
{ 
    public string Category { get; set; } 
    public int SessionNumber { get; set; } 
    public DateTime SessionStartDate { get; set; } 
} 

,並在您的操作方法,你可以使用Request.CreateResponse方法。

public HttpResponseMessage Get() 
{ 
    var db = new StudyDataContext(); 
    var data = db.details.Select(x => new CategoryDto { 
                Category = x.Category, 
                SessionStartDate = x.SessionStartDate, 
                SessionNumber = x.SessionNumber } 
           ).ToList(); 
    return Request.CreateResponse(HttpStatusCode.OK, data); 
} 
+0

謝謝。但其他的東西在控制器中應該都是一樣的。與Oracle Connection類似,給出查詢 – trx

+0

我以爲你想用EF實現替換所有的數據訪問代碼 – Shyju

+0

是的,我想替換它們。所以我們不需要任何'使用(OracleCommand selectCommand = new OracleCommand(strQuery,dbconn)) selectCommand.Parameters.AddRange(prms.ToArray()); (OracleDataAdapter adapter = new OracleDataAdapter(selectCommand)) { DataTable selectResults = new DataTable(); adapter.Fill(selectResults);'可能我有點困惑 – trx