【问题标题】:RavenDB indexes error "Could not find index named"RavenDB 索引错误“找不到名为的索引”
【发布时间】:2012-03-29 02:30:12
【问题描述】:

我不熟悉使用 RavenDB 并尝试让索引在一个简单的 MVC3 应用程序中工作,该应用程序允许用户输入地理位置。我有两个模型,一个 UserModel 和一个 LocationModel。 LocationModel 在保存时存储 UserId,我正在尝试在此创建索引。

public class Locations_ByUser : AbstractIndexCreationTask<LocationModel>
    {
        public Locations_ByUser()
        {
            Map = locations => from location in locations
                            select new { location.UserId };

        }
    }

我正在使用以下代码注册索引

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //ADD THE MODEL BINDER FOR LIST TO STRING
            ModelBinders.Binders.Add(typeof(TestAPI.Models.LocationModel), new TestAPI.Classes.LocationModelBinder());

            //INIT THE STORE, DO ONCE PER APP START
            TestAPI.Classes.DataDocumentStore.Initialize(); 

            //SET THE INDEXES
            IndexCreation.CreateIndexes(typeof(Locations_ByUser).Assembly, TestAPI.Classes.DataDocumentStore.Instance);
        }

但是,当我尝试从 mvc 应用程序调用索引时

[HttpGet]
        public ActionResult Index()
        {

            var result = this.DocumentSession.Query<LocationModel>("Locations_ByUser").ToList();
            foreach (var userid in result)
            {
                Console.Out.WriteLine(userid);
            }

            return View();
        }

它返回以下错误

找不到名为:Locations_ByUser 的索引

我想知道是否有其他人以前遇到过这种情况并可以为我指明正确的方向。提前致谢。

【问题讨论】:

    标签: .net asp.net-mvc-3 ravendb indexing


    【解决方案1】:

    RavenDB 中的索引在生成时实际上将被命名为:“Locations/ByUser”。

    如果您打开 Raven Studio,您可以在索引下看到它。 _ 替换为 /

    另外你不需要指定字符串值,你可以像这样写你的查询:

    var result = this.DocumentSession.Query<LocationModel, Locations_ByUser>().ToList();
    

    【讨论】:

    • 你太棒了!你设法为我解决了两个问题。我无法弄清楚如何让 Raven Studio 以嵌入式方式加载,并且由于这篇文章(以及让我的索引正常工作),我对其进行了跟踪。我一定错过了文档中的命名约定。你知道我在哪里可以找到它吗?
    • 我认为文档中没有提到索引命名,我认为这是在版本 4xx 期间进行的更改。不完全确定。我快速浏览了一下,没找到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多