我已按照以下链接从我的存储过程中获取多个结果集: Returning multiple resultsets with EntityFramework repository
以下是我的Repository类代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
//using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Data.SqlClient;
using System.Linq;
public class AccountRepository:DbContext, IAccountRepository
{
public AccountRepository(string inEnvironment) : base(inEnvironment)
{
Database.SetInitializer<AccountRepository>(null);
}
public AccountDetailsTemplate GetAccountSummary(long AccountId,int InstanceID)
{
var cmd = this.Database.Connection.CreateCommand();
cmd.CommandType= CommandType.StoredProcedure;
cmd.CommandText= "[dbo].[GetAccountDetail_SummaryByInstanceID]";
DbParameter paramAccountId = cmd.CreateParameter();
paramAccountId.ParameterName = "@AccountId";
paramAccountId.DbType = DbType.Int64;
paramAccountId.Direction = ParameterDirection.Input;
paramAccountId.Value = AccountId;
cmd.Parameters.Add(paramAccountId);
DbParameter paramInstanceID = cmd.CreateParameter();
paramInstanceID.ParameterName = "@InstanceID";
paramInstanceID.DbType = DbType.Int32;
paramInstanceID.Direction = ParameterDirection.Input;
paramInstanceID.Value = InstanceID;
cmd.Parameters.Add(paramInstanceID);
try
{
this.Database.Connection.Open();
var reader = cmd.ExecuteReader();
//Read AccountDetails
var accountDetails = ((IObjectContextAdapter)this.Database)
.ObjectContext
.Translate<AccountDetails>(reader).FirstOrDefault();
//Read AccountSummary
reader.NextResult();
var accountSummary= ((IObjectContextAdapter)this.Database)
.ObjectContext
.Translate<AccountSummary>(reader).FirstOrDefault();
AccountDetailsTemplate acctDetailsTemplate = new AccountDetailsTemplate();
acctDetailsTemplate.acctDetails = accountDetails;
acctDetailsTemplate.acctSummary = accountSummary;
return acctDetailsTemplate;
}
finally
{
this.Database.Connection.Close();
}
}
}
但是在执行代码时,我在以下行中收到错误:
//Read AccountDetails
var accountDetails = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<AccountDetails>(reader).FirstOrDefault();
我收到以下错误: 无法转换类型&System; System.Data.Entity.Database&#39;输入&#39; System.Data.Entity.Infrastructure.IObjectContextAdapter&#39;。&#39;
实体AccountDetails和AccountSummary不在我的DBContext类中。这是我收到此错误的原因吗?请帮我解决这个错误。