Wenn ich meine Entität erstellt habe, habe ich eine gespeicherte Prozedur importiert, die zwei Datensätze zurückgibt. Bei der Recherche fand ich heraus, wie man dies unter Verwendung nur von Code unterbringen kann und nicht das Entity-Modell xml ändert. Ich habe dies getan und jetzt ist mein erster Datensatz korrekt ausgefüllt. Mein zweiter Datensatz gibt korrekt 1 Zeile zurück, aber die Werte sind leer. Ich stellte sicher, dass die Schlüssel der Objekte die gleichen waren (Groß-/Kleinschreibung und Schreibweise) wie die Storeprozedur.Zweite Ergebnismenge gibt Nullwerte - linq an Entitäten zurück
Meine Ressourcen:
- 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.
Mein Code:
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();
}
Verwenden Sie Dapper für diese Art von Zeug. Entity Framework ist Overkill beim Aufruf gespeicherter Prozeduren und das Mischen von ExecuteReader macht wenig Sinn. –