2017-04-18 7 views
0

エンティティを作成したとき、2つのデータセットを返すストアドプロシージャをインポートしました。私が研究している間、私はコードだけを使用してこれを収容し、エンティティモデルxmlを変更しない方法を見つけました。私はこれをして、今私の最初のデータセットが正しく設定されています。私の2番目のデータセットは正しく1行を返しますが、値は空です。私はオブジェクトのキーがストアプロシージャが返すものと同じ(ケースとスペル)であることを確認しました。2番目の結果セットはnull値を返します - 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

ニース、ちょうどこれを考え出し、投稿していました。ありがとう! –

関連する問題