I am requesting a collection using Linq-MongoDB using the standard C # driver:
from n in db.GetCollection<EntityWrapper<Customer>>("Customers").AsQueryable()
where n.Entity.Names.Contains(name)
select n.Entity;
This throws an exception:
Class Customer does not have a member called names.
As you can see below, I am wrapping my objects to provide additional metadata without polluting my objects.
public class EntityWrapper<TEntity>
{
public TEntity Entity { get; set; }
public string CreatedBy { get; set; }
}
public class Customer : Entity
{
private List<string> names = new List<string>();
public IEnumerable<string> Names { get { return names; } }
public void AddName(string name)
{
names.Add(name);
}
}
I displayed a personal field:
BsonClassMap.RegisterClassMap<Customer>(n =>
{
n.AutoMap();
n.MapField("names");
});
I found the data in the database correct.
When I delete a private field and change the property Namesin List<string>, the request is executed normally. Obviously, I do not want to go this route, because I would jeopardize encapsulation.
Most simple queries work fine, like
from n in db.GetCollection<EntityWrapper<Customer>>("Customers").AsQueryable()
where n.Entity.Priority > 3
select n.Entity;
UPDATE
I found out a hack that makes the request work:
public IEnumerable<string> Names
{
get
{
return names;
}
private set { }
}
However, this is not close to acceptable.