无法执行实体框架方法

时间:2013-09-22 21:20:59

标签: c# linq entity-framework

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

任何帮助都将不胜感激。

堆栈追踪:

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

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

第100行:案例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(IEnumerable 1来源)+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);
      }
    }
  }

2 个答案:

答案 0 :(得分:0)

看起来你错过了execSqlQuery命令,它会执行存储过程,而不是尝试按该名称查询表。

尝试:

Case case1 = Context.Database.SqlQuery<Case>(
             "exec spCasesAdd @UserProfileID, @Title", userID, Title)
             .FirstOrDefault();

答案 1 :(得分:0)

问题解决了。我通过将更新移至业务层来解决此问题。