第二个结果集返回空值 - linq到实体

时间:2017-04-18 18:47:27

标签: c# entity-framework linq linq-to-entities

当我创建我的实体时,我导入了一个返回两个数据集的存储过程。在研究的过程中,我发现如何仅使用代码来解决这个问题,而不是修改实体模型xml。我已经完成了这项工作,现在我的第一个数据集已正确填充。我的第二个数据集正确返回1行,但值为空。我确保对象键与存储过程返回的相同(大小写和拼写)。

我的资源:

  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
  4. 我的代码:

    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 个答案:

答案 0 :(得分:2)

问题是,您的oEngine类具有公共字段,而EF映射(仅适用于属性

将其更改为

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

问题将得到解决。