Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Core/Grand.Data/LiteDb/LiteDBRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ public Task ClearAsync()
/// </summary>
public virtual IQueryable<C> TableCollection<C>() where C : class
{
return Database.GetCollection<C>(nameof(T)).Query().ToEnumerable().AsQueryable();
//typeof(T).Name, not nameof(T) - the latter is the literal "T"
return Database.GetCollection<C>(typeof(T).Name).Query().ToEnumerable().AsQueryable();
}

#endregion
Expand Down
5 changes: 4 additions & 1 deletion src/Core/Grand.Infrastructure/Startup/StartupApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ private void RegisterDataLayer(IServiceCollection serviceCollection, IConfigurat
var mongoUrl = new MongoUrl(connectionString);
var databaseName = mongoUrl.DatabaseName;
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
serviceCollection.AddScoped(_ => new MongoClient(clientSettings).GetDatabase(databaseName));
//the driver expects a single client per connection string - it owns the connection
//pool and the cluster monitoring, and is thread-safe
serviceCollection.AddSingleton<IMongoClient>(_ => new MongoClient(clientSettings));
serviceCollection.AddScoped(sp => sp.GetRequiredService<IMongoClient>().GetDatabase(databaseName));
}
else
{
Expand Down
14 changes: 14 additions & 0 deletions src/Tests/Grand.Data.Tests/LiteDb/LiteDbRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,18 @@ await myRepository.UpdateCollectionFieldItem("1", x => x.UserFields, z => z.Key
Assert.IsTrue(p.UpdatedOnUtc.HasValue);
Assert.AreEqual("user", p.UpdatedBy);
}

[TestMethod]
public void TableCollection_ReadsTheEntityCollection()
{
var myRepository = new LiteDBRepositoryMock<SampleCollection>();
myRepository.Insert(new SampleCollection { Id = "1", Name = "Test" });
myRepository.Insert(new SampleCollection { Id = "2", Name = "Test2" });

//reads the collection named after the entity, not one named "T"
var result = myRepository.TableCollection<SampleCollection>().ToList();

Assert.AreEqual(2, result.Count);
Assert.IsNotNull(result.FirstOrDefault(x => x.Id == "1"));
}
}
Loading