I have been building a basic MVC application and have gotten stumped on a couple things. The main one is connecting and finding something in mongodb. I have been trying to implement generics as well. Below I have provided some code to show you what I am trying to implement. My issue I believe is acquiring the correct collection from the database, but then the next issue I ran into was casting from T to User.
I have been going about different ways to try and figure this out, but I have been stumped for quite some time now. I implemented a Create function that uses the 'ConnectToMongo' function and that one works fine, but when I try to implement a find or findbyexpression and run into these errors. I hope someone can point me in the right direction, thank you.
/// <summary>
/// Finds the object by expression.
/// </summary>
/// <returns>The object by expression.</returns>
/// <param name="entity">Entity.</param>
/// <param name="expression">Expression.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
private async Task<T> FindObjectByExpression<T>(T entity, Expression<Func<T, bool>> expression)
{
T result;
try
{
var collection = this.ConnectToMongo(entity);
if (collection == null)
{
Console.WriteLine($"Collection: {entity.GetType()} does not exsit.");
return default(T);
}
var filterDefinition = Builders<T>.Filter.Where(expression);
result = (T)await collection.FindAsync(filterDefinition);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return default(T);
}
return result;
}
/// <summary>
/// Connects to mongo.
/// </summary>
/// <returns>The to mongo.</returns>
/// <param name="entity">Entity.</param>
private IMongoCollection<T> ConnectToMongo<T>(T entity)
{
IMongoCollection<T> collection = null;
try
{
MongoClientSettings setting = new MongoClientSettings
{
Server = new MongoServerAddress("localhost", 27017)
};
MongoClient client = new MongoClient(setting);
var mongoDbServer = client.GetDatabase(DBName);
collection = mongoDbServer.GetCollection<T>($"{entity.GetType().Name}");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
return collection;
}
public abstract class Command
{
public Command(Guid? ID)
{
if(ID == null)
{
ID = Guid.NewGuid();
}
else
{
this.ID = ID.Value;
}
}
[BsonId]
public Guid ID { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class User : Command
{
public User(Guid? Id)
: base(Id)
{
this.classDictionary = new Dictionary<string, string>();
}
[Required]
public Entitlement UserEntitlement { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string UniID { get; set; }
// Maps from Course ID to Section ID
public Dictionary<string, string> classDictionary { get; set; }
}
public enum Entitlement
{
Student = 10,
Admin = 20,
Bot = 30,
Unknown = 0
}
答案 0 :(得分:0)
我需要更改这一行
result = (T)await collection.FindAsync(filterDefinition);
此行
var results = await collection.Find(filterDefinition).ToListAsync();
result = results.FirstOrDefault();
它试图将IAsyncCursor强制转换为T。