与具有日期过滤器的OData一起使用时,RavenDB会抛出日期时间错误

时间:2015-04-17 21:57:29

标签: asp.net-web-api odata ravendb

我正在开发.Net项目,我使用Asp.Net Web Api和OData来查询Embedded Raven数据库。我有一个单独的存储库项目,其中放置了RavenDB的东西,我在Web Api项目中访问此存储库。

下面是如何使用虚拟数据设置RavenDB存储库:

    public class EmployeeRepository
    {
        private static readonly Lazy<IDocumentStore> Store = new Lazy<IDocumentStore>(CreateInstance);

        public static IDocumentStore DocumentStore
        {
            get { return Store.Value; }
        }

        private static IDocumentStore CreateInstance()
        {
            var documentStore = new EmbeddableDocumentStore
            {
                DataDirectory = "Data",
                UseEmbeddedHttpServer = false,
                DefaultDatabase = "EmployeeDocumentDB"
            }.Initialize();

            using (var session = documentStore.OpenSession())
            {
                //TODO: Generates test data and can be deleted.
                PopulateEmployeesIfStoreIsEmpty(session);
            }

            return documentStore;
        }

        private static void PopulateEmployeesIfStoreIsEmpty(IDocumentSession session)
        {
            if (!session.Query<Employee>().Any())
            {
                var document1 = new Employee
                {
                    Id=1,
                    JoiningDate = new DateTime(2015, 01, 15),
                    BirthDate = new DateTime(1980, 01, 01)
                };

                var document2 = new Employee
                {
                    Id = 2,
                    JoiningDate = new DateTime(2015, 02, 15),
                    BirthDate = new DateTime(1985, 01, 01)
                };


                session.Store(document1);
                session.Store(document2);

                session.SaveChanges();
            }
        }

        public IQueryable<Employee> GetEmployees()
        {
            using (var session = DocumentStore.OpenSession())
            {
                return session.Query<Employee>();
            }
        }
    }

以下是Employee类的外观:

public class Employee
{
    public int Id { get; set; }
    public DateTime JoiningDate { get; set; }
    public DateTime BirthDate { get; set; }
}

以下是ApiController的样子:

public class EmployeeController : ApiController
{
    [HttpGet]
    [Route("employee/list")]
    public IHttpActionResult GetEmployees(ODataQueryOptions<Employee> options)
    {
        try
        {
            EmployeeRepository employeeRepository = new EmployeeRepository();
            var queryable = options.ApplyTo(employeeRepository.GetEmployees(), new ODataQuerySettings
            {
                EnableConstantParameterization = false,
                HandleNullPropagation = HandleNullPropagationOption.False
            });

            var employees = RetriveEmployeeListFromQueryable(queryable);

            return Ok(employees);
        }
        catch (Exception e)
        {
            return Content(HttpStatusCode.InternalServerError, e.Message);
        }
    }

    private List<Employee> RetriveEmployeeListFromQueryable(IQueryable queryable)
    {
        var employees = new List<Employee>();
        var enumerator = queryable.GetEnumerator();

        while (enumerator.MoveNext())
        {
            var employee = enumerator.Current as Employee;
            employees.Add(employee);
        }
        return employees;
    }
}

我正在使用OData使用ApiController消耗上面的代码,当我在Web浏览器中触发查询时

http://localhost/employee/list?$filter=BirthDate eq 1985-01-01

我收到错误声明:

  

“无法理解如何翻译'IIF(($ it.JoiningDate.Kind == Utc)OrElse($ it.JoiningDate.Kind == Local)),new DateTimeOffset($ it.JoiningDate.ToUniversalTime() ).ToOffset(-06:00:00),new DateTimeOffset($ it.JoiningDate,(UTC-06:00)Central Time(US&amp; Canada)。GetUtcOffset($ it.JoiningDate))。ToUniversalTime()。ToOffset (-06:00:00))'到RavenDB查询。\ \你是否在查询期间进行计算?\ \ RavenDB在查询期间不允许计算,只允许在索引期间进行计算。考虑移动操作到索引。“

0 个答案:

没有答案