Linq2couchbase return empty

I am using linq2couchbase 2.0.0 in a .net 5 webapi. When I call GetAll(), it returns 0 object

Startup.cs
{
Configuration.GetSection(“Application”).GetSection(“CouchBase”).Bind(options);
options.AddLinq();
}).AddCouchbaseBucket(“strike1000”);

Repository.cs
public class CouchBaseRepository : IRepository
where E : IBaseEntity
{
private string collectionName;
private string scopeName = “_default”;
private BucketContext context;

    public CouchBaseRepository(INamedBucketProvider bucketProvider)
    {
        
        if (typeof(ILookup).IsAssignableFrom(typeof(E)))
        {
            scopeName = "lookups";
        }

        var attr = typeof(E).GetCustomAttributes(true).Where(w => w.GetType() == typeof(CollectionAttribute)).FirstOrDefault();
        collectionName = attr != null ? ((CollectionAttribute)attr).Name : typeof(E).Name;
        context = new BucketContext(bucketProvider.GetBucketAsync().Result);
    }


    public Task<E> Get(string id)
    {
        return Task.FromResult(context.Bucket.Collection(collectionName).GetAsync(id).Result.ContentAs<E>());
    }

    public Task<List<E>> GetAll()
    {
        return Task.FromResult(Query().ScanConsistency(Couchbase.Query.QueryScanConsistency.RequestPlus).ToList());
    }

    public async Task<E> Set(E entity)
    {
        if (string.IsNullOrEmpty(entity.Id))
        {
            entity.Id = ObjectId.GenerateNewId().ToString();
        }
        else
        {

        }
        var ret=await context.Bucket.Collection(collectionName).UpsertAsync(entity.Id, entity);
        //var ret = await col.UpsertAsync(entity.Id, entity);
        return entity;
    }

    public async Task<bool> Delete(E entity)
    {
        await context.Bucket.Collection(collectionName).RemoveAsync(entity.Id);
        return true;
    }

    public async Task<bool> Delete(string id)
    {
        await context.Bucket.Collection(collectionName).RemoveAsync(id);
        return true;
    }

    public IQueryable<E> Query()
    {
        return context.Query<E>();
    }

}

My record:
{
“serverId”: null,
“channelId”: null,
“status”: 0,
“keyword”: “QQQ”,
“expireDate”: null,
“winnerDate”: null,
“winner”: null,
“id”: “61dbe1e8816f264c649565ac”,
“createdBy”: null,
“updatedBy”: null,
“createdDate”: null,
“updatedDate”: null,
“type”: “KeywordRewardEntity”
}

@iRon

That’s interesting, can you give me an example entity E that you’re using this with? Most importantly, does it have any filter attributes, or just the CollectionName attribute?

Also, does your collection have a primary index? If you have no predicate this is required to support queries.

Finally, I’d like to warn you that you’ll have significant issues at scale (especially with .NET <= 5) with the way you’re using .Result so much. I’d strongly recommend waiting to construct the BucketContext until you’re in an async method rather than the constructor, using async/await, and use ToListAsync(), etc.

public class KeywordRewardEntity : BaseEntity
{
    public string ServerId { get; set; }
    public string ChannelId { get; set; }
    public KeyRewardStatusEnum Status { get; set; }
    public string Keyword { get; set; }
    public DateTime? ExpireDate { get; set; }
    public DateTime? WinnerDate { get; set; }
    public string Winner { get; set; }

}

public enum KeyRewardStatusEnum
{
    UNKNOWN = 0,
    OPEN = 1,
    VOID = 2,
    WINNER = 3
}

public class BaseEntity : IBaseEntity
{
public string Id { get; set; }
public string Type { get; set; }
public string CreatedBy { get; set; }
public string UpdatedBy { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
}

So, looking at this, I think your problem may be the collection attribute (or lack of it). In your constructor, you’re falling back to the class name when attribute is not present. However, that doesn’t match the behavior implemented by Linq2Couchbase. If there is no attribute present, it will query the default collection.

I removed the attribute and stll not working. And I am inserting the entity first before I count it, so it should be using same scope and collection:

 public void Test()
    {
        repository.Set(new KeywordRewardEntity { Keyword = "QQQ",Type= "KeywordRewardEntity" });
        var qq = repository.GetAll().Result;         
    }

I’ve figured it out. linq2couchbase will always use the default collection. To override you use their attribute

[CouchbaseCollection("_default", "KeywordRewardEntity")]
public class KeywordRewardEntity : BaseEntity

doc: Linq2Couchbase/scopes-collections.md at master · couchbaselabs/Linq2Couchbase · GitHub

Ah, yes, I thought that was the attribute you were using, but I see now that I look more closely that it wasn’t.