2017-04-18 8 views
0

엔티티를 만들 때 두 개의 데이터 집합을 반환하는 저장 프로 시저를 가져 왔습니다. 연구하면서 나는 코드 만 사용하고 엔터티 모델 xml을 수정하지 않는 방법을 발견했습니다. 지금까지 해본 결과 첫 번째 데이터 세트가 올바르게 채워졌습니다. 두 번째 데이터 세트가 올바르게 1 행을 반환하지만 값은 비어 있습니다. 개체 키가 저장 프로 시저가 반환하는 것과 동일 (대/소문자 및 철자)했는지 확인했습니다.두 번째 결과 집합은 엔티티에 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를 사용하십시오. Entity Framework는 저장 프로 시저를 호출 할 때 잔인하며 ExecuteReader를 혼합하면 의미가 거의 없습니다. –

답변

2

문제는 oEngine 클래스가 공공 필드를을 가지고있는 경우에만 특성와 EF지도 (작품) 동안.

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

변경을하고 문제가 해결 될 것입니다.

+1

니스, 그냥 이것을 알아 내고 게시했습니다. 감사! –