2017-04-18 47 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

不错,只是想出了这个,并张贴。谢谢! –