Confusing error "it's not persisted, so those attributes will be ignored" in Realm dotnet SDK

I had a property defined as

    [PrimaryKey]
    private ObjectId _id { get; } = ObjectId.GenerateNewId();

which caused the compilation warning:

SharedWith.cs(12,26,12,30): warning : Fody/Realm: SharedWith.Id has [MapTo], [PrimaryKey] applied, but it’s not persisted, so those attributes will be ignored.

It took me an annoyingly long time to realise what was wrong with the property declaration as I was writing it thinking as a C# developer - this should not be settable. I’d started with it as a public property with private set; and, for some reason, instead of specifying set; when I made it private, just left it as { get; }

It took a long time looking at this declaration vs other working code to realise the important semantic difference.

I’m adding this topic because I couldn’t find any discussion of the message elsewhere.

The correct declaration in full:

    [PrimaryKey]
    private ObjectId _id { get; set; } = ObjectId.GenerateNewId();

or could use

    [PrimaryKey]
    public ObjectId _id { get; private set; } = ObjectId.GenerateNewId();

In case anyone’s still lost - Realm requires the object always be at least privately settable even though it has an initialiser.

Hi @Andy_Dent, thanks for your post.
I agree that the error message is not very clear, we should probably improve it. I’ve created a ticket about it: Improve warning message for ignored property with attribute · Issue #3352 · realm/realm-dotnet · GitHub

1 Like