第二个结果集 returns 空值 - 对实体的 linq

Second result set returns null values - linq to entities

当我创建我的实体时,我导入了一个存储过程,其中 returns 两个数据集。在研究过程中,我发现了如何仅使用代码而不修改实体模型来解决这个问题 xml。我已完成此操作,现在我的第一个数据集已正确填充。我的第二个数据正确设置了 returns 1 行,但是值为空。我确保对象键与存储过程 returns.

相同(大小写和拼写)

我的资源:

  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();
}

问题是您的 oEngine class 有 public 字段 而 EF 映射(工作)仅适用于 属性.

改为

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

问题就解决了。