Search code examples
c#cassandracassandra-driver

CassandraEntityBase in C# DataStax Driver


I have a .net core application which persists data to a Cassandra instance through DataStax driver.

I have a base class for my Cassandra entities. Now if I want to take my TimeUUID type id into this base class, when inserting I get the error:

Some partition key parts are missing: id

Same approach works in EntityFramework. And also problem is not with my tables, connection or keyspace since when I carry the id field back to entty itself, it works.

My insert method

public void Insert<T>(T entity) where T : EntityBase
{
    _mapper.Insert(entity);
}

My base class

public class EntityBase
{
    [Column(name: "id")]
    public  TimeUuid Id { get; private set; } = TimeUuid.NewId();
}

An entity class which inherits base class

[Table(Keyspace = "unimportant", Name = "irrelevant")]
public class Irrelevant : EntityBase
{
    //fields
}

Can you tell me what is problem ? Is there a way to declare a CassandraEntityBaseClass ?


Solution

  • Found it. It was intentional. In PocoDataFactory.cs Line 102 I found it:

    return t.GetTypeInfo().GetProperties(PublicInstanceBindingFlags).Where(p => p.CanWrite);
    

    What is CanWrite ?

    Gets a value indicating whether the property can be written to.

    Source : PropertyInfo.CanWrite Property

    Since my property was readonly

    public  TimeUuid Id { get; private set; } = TimeUuid.NewId();
    

    I re-write my code(only to test) as below and it worked:

    public  TimeUuid Id { get; set;}
    

    I'll now choose to set my property during insert at repository or at object ctor.