0
當我創建我的實體時,我導入了一個存儲過程,它返回兩個數據集。在研究過程中,我發現如何僅使用代碼來適應此問題,而不是修改實體模型xml。我已經完成了這個,現在我的第一個數據集正確填充。我的第二個數據集正確返回1行,但值是空的。我確保對象鍵與存儲過程返回的相同(大小寫和拼寫)。第二個結果集返回空值 - linq到實體
我的資源:
- Issue when trying to read multiplte entity resultsets from a stored procedure
- How to get Multiple Result Set in Entity Framework using Linq with C#?
- 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();
}
對這種類型的東西使用Dapper。調用存儲過程時實體框架矯枉過正,而您對ExecuteReader的混合幾乎毫無意義。 –