How to use counter?

Hello,

I search an example on the using of a counter.

My goal is to implement an auto incremented ID with Entity Framwork.

I have added a custom generator on a model Blog like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)

{

    modelBuilder.Entity<Blog>().Property(b => b.Id).HasValueGenerator<IdGenerator<Blog>>();

}

And i try to implement the custom generator:

internal class IdGenerator<T>:ValueGenerator<T>

{

    public override T Next(EntityEntry entry)

    {

        var bucket = entry.Context.GetService<INamedBucketProvider>().GetBucketAsync().Result;

        return bucket.Increment($"{typeof(T).Name}Id")

    }
}

bucket.Increment is mentioned here but the compiler says that IBucket has no member Increment.

I use CouchbaseNetClient 3.9.0 and Couchbase.EntityFrameworkCore 1.0.1

Hi @CodeTroopers -

The KV API is located at the Collection level, so at a minumum you will have to do this:

return bucket.DefaultCollection().Increment($"{typeof(T).Name}Id").GetAwaiter().GetResult;

Or if you have a specific Scope and Collection you want to write to:

return bucket.Scope(“scope1”).Collection(“coll1”).Increment($"{typeof(T).Name}Id").GetAwaiter().GetResult`

As an aside, I would override the NextAsync method instead and use await/async instead of Next and GetAwaiter().GetResult.

-Jeff

Hi @jeffrymorris

Thank you for your reply, but now i have this error:

‘ICouchbaseCollection’ does not contain a definition for ‘Increment’ and no accessible extension method ‘Increment’ accepting a first argument of type ‘ICouchbaseCollection’ could be found