Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to register own Mapper? #818

Open
thedomeffm opened this issue Sep 16, 2024 Discussed in #816 · 1 comment
Open

How to register own Mapper? #818

thedomeffm opened this issue Sep 16, 2024 Discussed in #816 · 1 comment

Comments

@thedomeffm
Copy link

Probably an issue will receive some support..?

Discussed in #816

Originally posted by thedomeffm September 11, 2024
We want to use Guid as our primary key.

This fails because of: MappingFailedException Cannot map record to type Skill because the record does not contain a value for the property 'nodeId'.

What are we doing wrong?

using Neo4j.Driver.Mapping;

// ...

public class Skill
{
    [MappingSource("nodeId")]
    public Guid nodeId { get; set; }
    
    [Required]
    [MappingSource("name")]
    public string name { get; set; };
}
public class SkillRepository {
    // ...

    public async Task<Skill> Upsert(Skill skill) { /* ...  */ }

    public async Task<Skill> GetById(Guid nodeId)
    {
        StringBuilder queryBuilder = new StringBuilder("MATCH (skill:Skill {nodeId: $nodeId}) RETURN skill");
    
        IList<IRecord> result = await _neo4JDriverService.ExecuteReadQueryAsync(queryBuilder.ToString(), new { nodeId = nodeId.ToString() });
    
        RecordObjectMapping.Register(new GuidMapper()); // <-- Is this correct?

        RecordObjectMapping.RegisterProvider(new GuidMappingProvider()); // or this..?
    
        return result.Single().AsObject<Skill>();
    }
}
public class GuidMapper : IRecordMapper<Guid>
{
    public Guid Map(IRecord record)
    {
        // does not work:
        return Guid.Parse(record.ToString());
    }
}
public class GuidMappingProvider : IMappingProvider
{
    public void CreateMappers(IMappingRegistry registry)
    {
        registry
            .RegisterMapping<Skill>(
                builder =>
                {
                    builder
                        .UseDefaultMapping()
                        // does not work:
                        .Map(m => m.nodeId, record => Guid.Parse(record.Values["nodeId"].As<string>()))
                        // does not work:
                        .Map(m => m.nodeId, "nodeId", EntityMappingSource.Property, o => Guid.Parse(o.As<string>()), true)
                    ;
                });
    }
}
```</div>
@RichardIrons-neo4j
Copy link
Contributor

Hi there. I've responded to your discussion about this. There is indeed a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants