Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tobias Pettersson 19 posts 112 karma points
    Nov 06, 2021 @ 19:18
    Tobias Pettersson
    2

    Custom database table with one-to-many relationship not working.

    I'm trying to create two database tables using migrations and NotificationHandlers.

    The tables are supposed to have a one-to-many relationship. The DTO:s look like this. I've tried a few different options but I can't get anything to work:

    [TableName("Seasons")]
    [PrimaryKey("Id", AutoIncrement = true)]
    [ExplicitColumns]
    public class SeasonsSchema
    {
        [PrimaryKeyColumn(AutoIncrement = true, IdentitySeed = 1)]
        [Column("Id")]
        public int Id { get; set; }
    
        [Column("SeasonPageId")]
        public int SeasonPageId { get; set; }
    
        [Reference(ReferenceType.Many, ColumnName = "TournamentsId", ReferenceMemberName = "TournamentsId")]
        public List<TournamentsSchema> Tournaments { get; set; }
    }
    

    and

    [TableName("Tournaments")]
    [PrimaryKey("Id", AutoIncrement = true)]
    [ExplicitColumns]
    public class TournamentsSchema
    {
        [PrimaryKeyColumn(AutoIncrement = true, IdentitySeed = 1)]
        [Column("Id")]
        public int Id { get; set; }
    
        [Column("TournamentPageId")]
        public int TournamentPageId { get; set; }
    }
    

    this is the error thats thrown when starting the application

    error

    If I remove the

     [Reference(ReferenceType.Many, ColumnName = "TournamentsId", ReferenceMemberName = "TournamentsId")]
        public List<TournamentsSchema> Tournaments { get; set; }
    

    from the SeasonsSchema class it obviously work and both tables are created as they should, but there are no relationship between the two tables.

    I'm missing something I'm sure but I can't think of anything to try right now.

    I tried removing the Reference attribute and I've tried a bunch of things to get this to work but nothing have this far.

  • John Sharp 20 posts 110 karma points
    Jan 24, 2024 @ 16:07
    John Sharp
    0

    Hi Tobias,

    Did you ever solve this?

    John

  • Tobias Pettersson 19 posts 112 karma points
    Jan 25, 2024 @ 12:05
    Tobias Pettersson
    0

    Hi, unfortunately not.

    We solved it by going another route I think, was very long ago.

    Regards Tobias

  • John Sharp 20 posts 110 karma points
    Jan 25, 2024 @ 12:13
    John Sharp
    100

    Ah, no worries. Thanks for reply, I know it was s long while ago and was actually a long shot on my part.

    Honestly, I didn't think you'd reply so I dug into the code to find a solution. It turns out, deep in the bowels of the code there's a function that discovers the field names. If the field type is something that can be translated then you get the exception, its thrown by NPoco.

    The solution I discovered was to add [Ignore] to the properties or fields that are virtual or lookups. It needed to be the NPoco IgnoreAttribute as there are a few of them.

  • Tobias Pettersson 19 posts 112 karma points
    Jan 25, 2024 @ 12:32
    Tobias Pettersson
    1

    Ah, probably good to know anyways :)

    I'll mark that as an answer while also commenting that I have not tried it myself :)

    Thx!

Please Sign in or register to post replies

Write your reply to:

Draft