Copied to clipboard

Flag this post as spam?

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


  • Mark Drake 133 posts 457 karma points c-trib
    Oct 29, 2022 @ 08:00
    Mark Drake
    1

    NPoco Questions / Issues I've Ran Into

    Unable to Set a Foreign Key Constraint on ContentTypeDto.

    This class is marked as internal, so I cannot leverage it in the same way I create foreign key restraints elsewhere.

    image

    I've created similar restraints using UserDto and NodeDto. I suppose my questions are:

    1. Am I creating foreign key restraints correctly?
    2. Was ContentTypeDto intended to be private?
    3. Does a workaround exist?


    Migration Fails If My Table Schema Includes a List<T>.

    I will gladly come back to this and share some sample code.

    Has anyone made the following schema work?

    public class Book {
       public List<Author> Authors { get; set; }
    }
    
    public class Author {
      public string Name { get; set; }
    }
    

    My attempts to use any List<T> has resulted in a failed migration, preventing the site from booting.


    [RESOLVED] Unable to Set a Composite Primary Key with Npoco.

    RESOLVED To use a composite primary key, one must annotate a single property with a particular attribute from Umbraco's Database Annotations. I'm confused as to why NPoco's approach does not work. Something like this should be documented, at the very least. If NPoco is not fully adopted/supported, why use it?

    Let's say I have a schema defined as such:

    [TableName("Example")]
    [PrimaryKey("One,Two")]
    
    public class Example {
    
      public int One {get;set;}    
      public int Two {get;set;}
    
    }
    

    And I create the table as such:

    Create.Table<Example>().Do();
    

    When I inspected my new table, the primary key had not been created.

    image

    I don't pretend to have the faintest clue what I'm doing regarding NPoco. Could someone kindly tell me if I'm doing something wrong? I'm under the impression that a call to Create.Table<>() should create all the necessary keys based on the attributes I've assigned.

    I can manually create Primary Keys via Create.PrimaryKey(), which works. I'd like to understand why my attributes do not work as intended.

  • milkman matty 31 posts 125 karma points
    May 29, 2023 @ 04:38
    milkman matty
    0

    With your help I was able to find an example of a composite key in the source code. Leaving this here for anyone else that needs to see a full example (this is of the UserGroup2NodePermission table):

    [TableName(Constants.DatabaseSchema.Tables.UserGroup2NodePermission)]
    [ExplicitColumns]
    internal class UserGroup2NodePermissionDto
    {
        [Column("userGroupId")]
        [PrimaryKeyColumn(AutoIncrement = false, Name = "PK_umbracoUserGroup2NodePermission", OnColumns = "userGroupId, nodeId, permission")]
        [ForeignKey(typeof(UserGroupDto))]
        public int UserGroupId { get; set; }
    
        [Column("nodeId")]
        [ForeignKey(typeof(NodeDto))]
        [Index(IndexTypes.NonClustered, Name = "IX_umbracoUser2NodePermission_nodeId")]
        public int NodeId { get; set; }
    
        [Column("permission")]
        public string? Permission { get; set; }
    }
    

    Really the thing doing the heavy lifting here is this attribute:

    [PrimaryKeyColumn(AutoIncrement = false, Name = "PK_umbracoUserGroup2NodePermission", OnColumns = "userGroupId, nodeId, permission")]
    
Please Sign in or register to post replies

Write your reply to:

Draft