0

我收到以下异常:异常详细信息:System.InvalidOperationException:无法执行该方法,因为方法本身或包含类型未完全实例化。

任何帮助,将不胜感激。

堆栈跟踪:

第 98 行:SqlParameter userID = new SqlParameter("@UserProfileID", user.ID);

第 99 行:SqlParameter Title = new SqlParameter("@Title", user.FirstName + "Inventory");

第 100 行:Case case1 = Context.Database.SqlQuery("spCasesAdd @UserProfileID, @Title", userID, Title).FirstOrDefault();

第 101 行:tranScope.Complete();

第 102 行:}

源文件:c:\Development\UI\ViewModels\NewAgencyViewModel.cs 行:100

System.Data.Entity.Internal.InternalContext.GetStateEntries() +0 System.Data.Entity.Internal.InternalSqlNonSetQuery.GetEnumerator() +102 System.Data.Entity.Internal.InternalSqlQuery 1.GetEnumerator() +41 System.Linq.Enumerable.FirstOrDefault(IEnumerable1 来源) +152

类的代码:

  public class NewAgencyViewModel
  {
    [Required]
    [StringLength(150, MinimumLength = 6)]
    [Display(Name = "Agency Name:")]
    public string AgencyName { get; set; }

    [Required]
    [StringLength(50, MinimumLength = 2)]
    [Display(Name = "Contact First Name:")]
    public string FirstName { get; set; }

    [Display(Name = "Contact Middle Name:")]
    [StringLength(50, MinimumLength = 2)]
    public string MiddleName { get; set; }

    [Required]
    [Display(Name = "Contact Last Name:")]
    [StringLength(50, MinimumLength = 2)]
    public string LastName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Contact Email Address:")]
    [Remote("VerifyUniqueEmail", "Home")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New Password:")]
    public string NewPassword { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Confirm New Password:")]
    [System.ComponentModel.DataAnnotations.Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    public void Update(InvContext Context)
    {
      AgencyRepository agencyRepo = new AgencyRepository(Context);
      Agency agency = agencyRepo.Get(-1);
      if (agency == null)
        throw new System.Exception("There Is No Template Agency Available To Process Your Header Image.  Please Try Again Later");
      byte[] headerImage = agency.HeaderImage;
      byte[] backgroundImage = agency.BackgroundImage;

      // Create the Agency
      try
      {
        agency = new Agency();
        agency.Name = AgencyName;
        agency.Email = Email;
        agency.Actve = true;
        agency.HeaderImage = headerImage;
        agency.BackgroundImage = backgroundImage;
        agencyRepo.Add(agency);
        agencyRepo.Save();
        if (agency.ID < 1)
        {
          throw new System.Exception("Your Agency Could Not Be Created.  Please Try Again Later");
        }
      }
      catch (DbEntityValidationException e)
      {
        throw new System.Exception(e.InnerException == null ? e.Message : e.InnerException.Message);
      }
      catch (Exception e)
      {
        throw new System.Exception(e.InnerException == null ? e.Message : e.InnerException.Message);
      }

      // Create the new User Profile
      try
      {
        string token = WebSecurity.CreateUserAndAccount(Email, NewPassword, new
        {
          RegistrationCode = "",
          FirstName = FirstName,
          MiddleName = MiddleName == null ? "" : MiddleName,
          LastName = LastName,
          AgencyID = agency.ID,
          LastLogonDate = SqlDateTime.MinValue,
          Active = true,
          PostDisaster = false
        });
      }
      catch (Exception e)
      {
        throw new System.Exception(e.InnerException == null ? e.Message : e.InnerException.Message);
      }

      // Add the User to the Agent role
      try
      {
        Roles.AddUserToRole(Email, "Agent");
      }
      catch (Exception e)
      {
        throw new System.Exception(e.InnerException == null ? e.Message : e.InnerException.Message);
      }

      // Create the Inventory List for the new User
      try
      {
        UserProfileRepository upRepo = new UserProfileRepository(Context);
        GinkoBL.UserProfile user = upRepo.Get(Email);
        CaseRepository caseRepo = new CaseRepository(Context);

        SqlParameter userID = new SqlParameter("@UserProfileID", user.ID);
        SqlParameter Title = new SqlParameter("@Title", user.FirstName + " Inventory");
        Case case1 = Context.Database.SqlQuery<Case>("spCasesAdd @UserProfileID, @Title", userID, Title).FirstOrDefault();
        if (case1.ID < 1)
        {
          throw new System.Exception("Your Agency Could Not Be Created.  Please Try Again Later");
        }
      }
      catch (Exception e)
      {
        throw new System.Exception(e.InnerException == null ? e.Message : e.InnerException.Message);
      }
    }
  }
4

2 回答 2

0

问题解决了。我通过将更新移动到业务层解决了这个问题。

于 2013-09-23T16:40:13.133 回答
0

看起来好像您缺少执行存储过程而不是尝试按该名称查询表的exec命令。SqlQuery

尝试:

Case case1 = Context.Database.SqlQuery<Case>(
             "exec spCasesAdd @UserProfileID, @Title", userID, Title)
             .FirstOrDefault();
于 2013-09-23T04:03:24.617 回答