jqGrid的&安培; ASP.NET MVC 4:如何使一个的DbContext库和“计算”属性搜索的实现? [英] jqGrid & ASP.NET 4 MVC: How to make search implementation on a DBContext repository and 'calculated' properties?

查看:74
本文介绍了jqGrid的&安培; ASP.NET MVC 4:如何使一个的DbContext库和“计算”属性搜索的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施MVC jqGrid的搜索,继@Oleg有趣的答案,关于这个问题:<一href=\"http://stackoverflow.com/questions/5500805/asp-net-mvc-2-0-implementation-of-searching-in-jqgrid\">ASP.NET MVC 2.0实施的jqGrid 搜索的。

I'm trying to implement jqgrid search on MVC, following the interesting answer by @Oleg, regarding the question: ASP.NET MVC 2.0 Implementation of searching in jqgrid.

其实我有一个基于EF和放一个数据存储库;的DbContext。此外,我有'计算'领域,我的意思是被其他领域的基础上,计算出DbSets属性的实体。

Actually I have a data repository based on EF & DBContext. Moreover, I have an entity with 'calculated' fields, I mean properties in DbSets that are calculated on the base of other fields.

我有两个主要问题,实施在上述链路的第一个答案描述的解决方案:

I have two main problems, implementing the solution described in the first answer of the above link:

第1问题)的解决方案是基于的ObjectQuery。我相信我已经通过创建我的上下文的IObjectContextAdapter,然后用(的ObjectQuery)铸造解决......在我的无知,我完全不知道,如果这个解决方案可以被看作是可扩展的,或是否有更好的解决办法......我我相信它的存在,但它已经超出了我的知识!

1st problem) The solution is based on ObjectQuery. I believe I have solved by creating a IObjectContextAdapter of my context and then casting with (ObjectQuery)...in my ignorance, I do not exactly know if this solution may be regarded as scalable or if there is a better solution...I am sure it exists, but it is beyond my knowledge!

第二个问题)首先查询,以下EntitySqlException升高:'计算'不是类型的成员Models.Ticket在当前加载方案

2nd problem) At first query, the following EntitySqlException is raised: 'Calculated' is not a member of type 'Models.Ticket' in the currently loaded schemes

愿你给我一些什么样的帮助或建议上述问题,好吗?

May you give me some kind of help or suggestion to above problems, please?

下面我把code的某些部分,我认为可以澄清:

Here I put some parts of code I think could clarify:

公开ENUM
 公共枚举StatiTT:INT {A = 1,B = 2,C = 3,D = 4,E = 5,F = 6,G = 7};

PUBLIC ENUM public enum StatiTT : int { A = 1, B = 2, C = 3, D = 4, E = 5, F = 6, G = 7 };

TICKET实体

 public class Ticket : IValidatableObject
 {

  public DateTime Data1 { get; set; }    
  public int StatoTicketID { get; set; }
....
  public int Calculated  // here's the problem...this is not a real field, it's a calculated property, as you see...
   {
      get
       {
           int mm=0;

           DateTime Ora = DateTime.Now;

           mm = (Data1 - Ora).Days*1440 + (Data1 - Ora).Hours * 60 + (Data1 - Ora).Minutes;

           if (StatoTicketID > (int)StatiTT.DI && mm < 0) mm = 10000000; 

           return mm;
       }
   }

背景

public class dbContext : DbContext
   {

       public DbSet<Ticket> Tickets{ get; set; }
     ........

*的库(实际上是在上述溶液中不使用)*

public class myRepository : ImyRepository, IDisposable
{
    private dbContext context;

    public myRepository(dbContext context)
    {
        this.context = context;
    }

    public IQueryable<Ticket> ListTicketsQ()
    {
        return (from e in context.Tickets select e);
    }
    ..........

控制器

     public JsonResult jqIndex(string sidx, string sord, int page, int rows, bool _search, string filters)
    {
        var context = new dbContext();
        var objectContext = ((IObjectContextAdapter)context).ObjectContext;
        var set = objectContext.CreateObjectSet<Ticket>();

        var serializer = new JavaScriptSerializer();
        Filters f = (!_search || string.IsNullOrEmpty(filters)) ? null : serializer.Deserialize<Filters>(filters);
        ObjectQuery<Ticket> filteredQuery =
            (f == null ? (ObjectQuery<Ticket>)set : f.FilterObjectSet((ObjectQuery<Ticket>)set));
        filteredQuery.MergeOption = MergeOption.NoTracking; // we don't want to update the data
        var totalRecords = filteredQuery.Count();

        var pagedQuery = filteredQuery.Skip("it." + sidx + " " + sord, "@skip",
                                            new ObjectParameter("skip", (page - 1) * rows))
                                     .Top("@limit", new ObjectParameter("limit", rows));
        // to be able to use ToString() below which is NOT exist in the LINQ to Entity
        var queryDetails = (from item in pagedQuery
                            select new {     
                                            item.Calculated, // << THIS 'property' RAISES EntitySqlException
                                            }).ToList();
         .....

任何帮助将是AP preciated。非常感谢你!

Any help would be appreciated. Thank you very much!

推荐答案

最佳工作解决我的问题已经解决了以下@Oleg先生的precious提示:我的移动计算性能到SQL Server ,创建计算列每个属性即可。现在,它工作正常,这是真快!

The best working solution to my problem has been resolved following the precious hints of Mr @Oleg: I moved the calculated properties into SQL Server, creating Computed Columns for each property. Now it works fine and it is really fast!

我失去了更多的时间试图让工作与对象集计算的特性,不是直接在数据库中创建新的计算列!正如奥列格正确指出的,简单的事总是最棒的!

I lost more time trying to get working the calculated properties with ObjectSet, than create new computed columns directly in the db! As rightly pointed by Oleg, simple things are always the best!

又一个暗示,对于谁在使用EF codefirst:如果你想使用计算机属性,必须分贝创建后删除列,并把 [DatabaseGenerated(DatabaseGeneratedOption.Computed)] 属性上面属性,如指定这里了。

Just another hint, for who's using EF Codefirst: if you want to use computed properties, you must DROP COLUMNS after db creation and putting [DatabaseGenerated(DatabaseGeneratedOption.Computed)] attribute above property, as specified in here too.

非常感谢你奥列格!我希望这个解决方案可以帮助其他人!

Thank you very much Oleg! I hope this solution may help other people!

这篇关于jqGrid的&安培; ASP.NET MVC 4:如何使一个的DbContext库和“计算”属性搜索的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆