Hey guys, first time posting on here so I hope I’m in the right place…
So I’m trying to set up a backlink on this Metal RealmObject using Realm Sync.
public class Metal : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("Name")]
[Required]
public string Name { get; set; }
[MapTo("Bare")]
[Backlink(nameof(Bara.TipMetal))]
public IQueryable<Bara> Bare { get; }
public class Bara : RealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("Name")]
[Required]
public string Name { get; set; }
[MapTo("TipMetal")]
public Metal TipMetal { get; set; }
}
I’m assigning Bara.Tipmetal in the Bara constructor, it works, shows up in the database.
My problem is I can’t find out how to assign it to IQueryable, this doesn’t even show up in my schema.
When working with backlinks you don’t actually need to assign them, as it’s automatically done by Realm. What I mean here is that once you assign Bara.TipMetal, then the IQueryable will be automatically populated.
Something like this:
var myMetal = new Metal { Name = "iron" } ;
var myBara = new Bara
{
Name = "bara1"
TipMetal = myMetal
}
realm.Write( () => {
realm.Add(myBara);
}
var baraName = myMetal.Bare.First().Name <---- this will be "bara1", it's automatically populated
Please note that there could be some errors as I’m writing this from the top of my head. I hope that this will make things clearer.
Thank you so much for your answer and quick reply!
I thought it worked like you just said, I must have messed up the code when pulling data from the DB.
It was confusing to me because in my realm schema I don’t see a Metal.Bare (Mapped to “Bare”) field show up but I can get the data with no trouble now.