存储过程首先使用实体​​框架6代码

时间:2015-04-24 07:15:29

标签: asp.net-mvc entity-framework stored-procedures

我有一个带有实体框架6的Asp Mvc5应用程序,我使用代码第一种方法。我的模型定义如下:

public class EntityBase 
{
 [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int Id {get; set; }
}
    public class Question : EntityBase
    {

      public QuestionTypeEnum QuestionType {get; set; } 
    }

     public enum QuestionTypeEnum
    {
    none = 0,
    Type1 = 1,
    Type2 = 2
    .
    .
    }
    public class Answer : EntityBase
    {
      public Question Answers {get; set; }
      public userInfo userInfo {get; set; } 
      public bool Isvalidated {get; set;}
      public FilterTypeEnum FitlterType
    }

 public enum FilterTypeEnum 
    {
    filter1 = 0,
    filter2 = 1
    } 

    public class UserInfo : EntityBase
    {
      public UserInfoTypeEnum UserInfoType {get; set; }
    }

   public enum UserInfoTypeEnum 
        {
          type1 = 0,
          type2 = 1,
          type3 = 2
        } 
    public class Company : EntityBase
    {
      public List<Answer> Answers {get; set; }
      public userInfo userInfoAdmin {get; set; } 
    }

我有somme perfermance问题,我想将以下方法转换为stoked过程:

public List<Answer> FilterCompanyAnswers(FilterTypeEnum filter, List<Answer> companyAnswers)
        {
            var answers = companyAnswers.Where(a => ((a.Question.QuestionType != QuestionType.none &&
                                                  a.UserInfo != null) &&
                                                  (((a.UserInfo.UserGroup == UserGroupEnum.type1 ||
                                                    (a.UserInfo.UserGroup == UserGroupEnum.type2 && a.IsValidated)) &&
                                                    a.AnswerTypeId == (int)filter &&
                                                   filter == AnswerTypeEnum.filter1) ||
                                                 ((a.UserInfo.UserGroup == UserGroupEnum.type3 ||
                                                   a.UserInfo.UserGroup == UserGroupEnum.type1 ||
                                                   a.UserInfo.UserGroup == UserGroupEnum.type2) &&
                                                   a.FilterTypeEnum  == (int)filter &&
                                                 filter == AnswerTypeEnum.filter2))) ||
                                                 (a.Question.QuestionType == QuestionTypeEnum.none))
                                                 .OrderByDescending(a => a.DateCreate)
                                    .GroupBy(a => a.QuestionId)
                                    .Select(grp => grp.First())
                                    .ToList();
            return answers;
        }

我怎样才能实现这一目标? 提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用db.Database.SqlQuery创建存储过程并调用它。 db.Database.SqlQuery('exec myStoredProcedure').ToList<MatchingType>()

结果集必须可转换为MatchingType。

您可以查看详细信息herehere