Hi,
I got some problem when developing my dotnet web application with MongoDB.EntityFrameworkCore,
When I have querying data with specific condition, it won’t work
using Microsoft.EntityFrameworkCore;
using MongoDB.Bson;
using Application.Domain.Aggregate;
using Application.Domain.Repository;
namespace Application.Infrastructure.Core.Data.Impl;
public class MemberInfoMongoRepository : IMemberInfoMongoRepository
{
private readonly MemberInfoContext _memberInfoContext;
public MemberInfoMongoRepository(MemberInfoContext memberInfoContext)
{
_memberInfoContext = memberInfoContext;
}
public async Task<MemberInfo> GetMemberInfoAsync()
{
//data load into _memberInfoContext.MemberInfo for sure
//get 10 row correct data
var test1 = await _memberInfoContext.MemberInfo.OrderByDescending(c => c.Id).Take(10).AsNoTracking().ToListAsync();
//get correct data
var test2 = await _memberInfoContext.MemberInfo.FirstOrDefaultAsync();
//null
var test3 = await _memberInfoContext.MemberInfo.Where(x => x.MemberEmail == "tUryw34u7r8ko@gmail.com").AsNoTracking().ToListAsync();
//null
var test4 = await _memberInfoContext.MemberInfo.Where(x => x.MemberEmail.Equals("tUryw34u7r8ko@gmail.com")).AsNoTracking().ToListAsync();
//null
var test5 = await _memberInfoContext.MemberInfo.AsNoTracking().FirstOrDefaultAsync(x => x.MemberEmail == "tUryw34u7r8ko@gmail.com");
return test2 ;
}
}
but the document says normal Efcore API works,but I tried over and over again,still don’t know what’s wrong
and also print log in my console
info: Microsoft.EntityFrameworkCore.Database.Command[35000]
Executed MQL query
my_member.member_info.aggregate([{ "$sort" : { "_id" : -1 } }, { "$limit" : NumberLong(10) }])
info: Microsoft.EntityFrameworkCore.Database.Command[35000]
Executed MQL query
my_member.member_info.aggregate([{ "$limit" : NumberLong(1) }])
info: Microsoft.EntityFrameworkCore.Database.Command[35000]
Executed MQL query
my_member.member_info.aggregate([{ "$match" : { "MemberEmail" : "tUryw34u7r8ko@gmail.com" } }, { "$limit" : NumberLong(1) }])
info: Microsoft.EntityFrameworkCore.Database.Command[35000]
Executed MQL query
my_member.member_info.aggregate([{ "$match" : { "MemberEmail" : "tUryw34u7r8ko@gmail.com" } }, { "$limit" : NumberLong(1) }])
info: Microsoft.EntityFrameworkCore.Database.Command[35000]
Executed MQL query
my_member.member_info.aggregate([{ "$match" : { "MemberEmail" : "tUryw34u7r8ko@gmail.com" } }, { "$limit" : NumberLong(1) }])
and some other settings
using MongoDB.EntityFrameworkCore;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
[Collection("member_info")]
public class MemberInfo
{
public ObjectId Id { get; set; }
public long MemberId { get; set; }
public string MemberEmail{ get; set; }
}
using Microsoft.EntityFrameworkCore;
using Application.Domain.Aggregate;
namespace Application.Infrastructure.Core.Data;
public class MemberInfoContext : DbContext
{
public DbSet<MemberInfo> MemberInfo { get; set; }
public MemberInfoContext(DbContextOptions options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MemberInfo>();
}
}
Program.cs
var mongoDBSettings = config.GetSection("MongoDBSettings").Get<MongoDBSettings>();
services.Configure<MongoDBSettings>(config.GetSection("MongoDBSettings"));
services.AddDbContext<MemberInfoContext>(options =>
{
options.UseMongoDB(mongoDBSettings.ConnectionString, mongoDBSettings.Database)
.UseLoggerFactory(LoggerFactory.Create(builder => { builder.AddConsole(); }))
.EnableSensitiveDataLogging();
});
project version: 8.0
MongoDB.EntityFrameworkCore nuget package version: 8.1.0 、8.1.1
db version: 7.0.5