不确定必须传递什么objectContext

时间:2012-11-26 09:16:21

标签: c# asp.net-mvc-3

我有资料库:

namespace Zinc.Repositories.EF.Scorm
{
public class ScormModuleRepository : Repository<ScormModule>, IScormModuleRepository
{
#region Constructor

/// <summary>
/// Initializes a new instance of the <see cref="ScormModuleRepository"/> class.
/// </summary>
/// <param name="objectContext">The object context.</param>
public ScormModuleRepository(ObjectContext objectContext)
  : base(objectContext)
{

}

#endregion

/// <summary>
/// Clear all records (for user and selected scorm module) from the scorm.userscormmoduledata and scorm.userscormmoduleassessment tables.
/// </summary>
/// <param name="id"></param>
/// <param name="userScormModuleId"></param>
public void ResetModule(int id, int userScormModuleId, int currentUser)
{
  using (SqlConnection conn = new SqlConnection(ZincModelContainer.CONNECTIONSTRING))
  {
    using (SqlCommand cmd = conn.CreateCommand())
    {
      conn.Open();
      cmd.CommandType = System.Data.CommandType.StoredProcedure;
      cmd.CommandText = "[Zinc].[ResetUserScormModuleData]";

      SqlParameter param = new SqlParameter("@userId", System.Data.SqlDbType.Int);
      param.Value = id;
      cmd.Parameters.Add(param);

      SqlParameter param2 = new SqlParameter("@userScormModuleId", System.Data.SqlDbType.Int);
      param2.Value = userScormModuleId;
      cmd.Parameters.Add(param2);

      SqlParameter param3 = new SqlParameter("@currentUser", System.Data.SqlDbType.Int);
      param3.Value = currentUser;
      cmd.Parameters.Add(param3);

      cmd.ExecuteNonQuery();
    }
  }
}

在我的控制器中我有:

[HttpGet]
public ActionResult UserDetails(int id)
{
  var user = ZincService.GetUserForId(id);
  if (user == null || user.Customer.CustomerId != CurrentCustomer.CustomerId)
    return DataNotFound();

  ManageUserViewModel viewModel = new ManageUserViewModel();
  viewModel.User = user;
  viewModel.IsLockedOut = MembershipService.IsUserLocked(user.Email);
  viewModel.IsActivated = user.DateTimeActivated.HasValue && MembershipService.IsUserApproved(user.Email);
  viewModel.IsArchived = user.IsArchived;
  var scormModuleRepository = new ScormModuleRepository();
  var trainingService = new Zinc.ServiceImplementations.TrainingService(scormModuleRepository);
  IEnumerable<UserTrainingDataModel> ids =  trainingService.GetTrainingModulesForUser(user.UserId);
  viewModel.TrainingModules = ids;

  return View(viewModel);
}

我收到错误:var scormModuleRepository = new ScormModuleRepository();说明:

  

错误2'Zinc.Repositories.EF.Scorm.ScormModuleRepository'没有   包含一个取0的构造函数   参数C:\ TFSPreview \ Zinc \ Web \ Project \ Areas \ User \ Controllers \ ManageController.cs 102 35 Zinc.Web

我必须在这里传递什么类型的ObjectContext?

1 个答案:

答案 0 :(得分:1)

是的,您必须将ObjectContext的实例传递给ScormModuleRepository的构造函数

// get context
ObjectContext objectContext = ....;
var repository = new ScormModuleRepository(objectContext);