This project has moved and is read-only. For the latest updates, please go here.

Call to 'First' method hangs

Jan 20, 2014 at 6:39 AM
Edited Jan 20, 2014 at 6:40 AM
Hi,

I'm trying MongoRepository for the first time in my ASP.NET MVC app.
I have installed mongodb, created a database, a collection (MyEntity) and added couple of documents in that collection (e.g {Name: "xyz"} )

In my MVC app, I added reference to MongoRepository.
created an entity class as below:
public class MyEntity : Entity
{
  public string Name { get; set; }
}
and in my controller's Index() method, i'm doing this.
MongoRepository<MyEntity> repo = new MongoRepository<MyEntity>();
repo.First(x=> x.Name = "xyz");
But the program hangs right at the 'First' method call. No exception nothing. What is that i'm doing wrong?

Below is the connectionstring from my web.config
<connectionStrings>
    <add name="MongoServerSettings" connectionString="mongodb://localhost/newdatabase/>
  </connectionStrings>
and yes, mongodb server is running.

Thanks,
Jan 20, 2014 at 7:06 AM
You are assigning (x.Name = "xyz") instead of comparing (x.Name == "xyz").
Jan 20, 2014 at 9:58 PM
Hi RobIII,

Sorry, it was a typo. I did have (x.Name == "xyz"), but doesn't work.

Thanks,
Jan 21, 2014 at 8:07 AM
This works fine for me:
using MongoRepository;
using System;
using System.Linq;

namespace Test513442
{
    class Program
    {
        static void Main(string[] args)
        {
            var repo = new MongoRepository<MyEntity>();
            repo.Add(new MyEntity() { Name = "Foo" });
            repo.Add(new MyEntity() { Name = "Bar" });
            repo.Add(new MyEntity() { Name = "xyz" });
            repo.Add(new MyEntity() { Name = "Baz" });

            var result = repo.First(x => x.Name == "xyz");
            Console.WriteLine(result.Id);
        }
    }

    public class MyEntity : Entity
    {
        public string Name { get; set; }
    }
}
Could you please post a simplified version of your project that reproduces the problem? Make sure there's just enough code to reproduce the problem reliably, no more, no less.
Jan 24, 2014 at 3:54 AM
Hi RobIII,

I observed that one of the cmd promts was open and was connected to my mongo database. I closed it and tested my app again. This time it works!.

But it sounded weird, so I opened another cmd prompt connected to my mongo database and tried my application again. It works without complaining.

I wasn't able to figure out what caused the issue in the first place. Still investigating.

Anyways, thanks for your prompt replies.

Cheers,