2017-04-18 86 views
0

當我創建我的實體時,我導入了一個存儲過程,它返回兩個數據集。在研究過程中,我發現如何僅使用代碼來適應此問題,而不是修改實體模型xml。我已經完成了這個,現在我的第一個數據集正確填充。我的第二個數據集正確返回1行,但值是空的。我確保對象鍵與存儲過程返回的相同(大小寫和拼寫)。第二個結果集返回空值 - linq到實體

我的資源:

  1. Issue when trying to read multiplte entity resultsets from a stored procedure
  2. How to get Multiple Result Set in Entity Framework using Linq with C#?
  3. https://msdn.microsoft.com/en-us/library/jj691402(v=vs.113).aspx

我的代碼:

public class oEngine 
{ 
    public string Engine; 
    public DateTime ResultsDateTime; 
} 

... 

// If using Code First we need to make sure the model is built before we open the connection 
// This isn't required for models created with the EF Designer 
ctx.Database.Initialize(force: false); 

// Create a SQL command to execute the sproc 
var cmd = ctx.Database.Connection.CreateCommand(); 
cmd.CommandText = "[dbo].[usp_IntMonDisplay]"; 

try 
{ 
    ctx.Database.Connection.Open(); 
    // Run the sproc 
    var reader = cmd.ExecuteReader(); 

    // Read Blogs from the first result set 
    reply.results = ((IObjectContextAdapter)ctx).ObjectContext.Translate<Entities.usp_IntMonDisplay_Result>(reader).ToList(); 

    // Move to second result set and read Posts 
    reader.NextResult(); 
    reply.engines = ((IObjectContextAdapter)ctx).ObjectContext.Translate<oEngine>(reader).ToList(); 

} 
finally 
{ 
    ctx.Database.Connection.Close(); 
} 
+1

對這種類型的東西使用Dapper。調用存儲過程時實體框架矯枉過正,而您對ExecuteReader的混合幾乎毫無意義。 –

回答

2

的問題是,你的oEngine類有公共領域而EF地圖(工程)只與性質

將其更改爲

public class oEngine 
{ 
    public string Engine { get; set; } 
    public DateTime ResultsDateTime { get; set; } 
} 

和問題就解決了。

+1

不錯,只是想出了這個,並張貼。謝謝! –