Copied to clipboard

Flag this post as spam?

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


  • Max 80 posts 437 karma points
    Apr 08, 2017 @ 16:20
    Max
    0

    custom tables with umbracoUser Foreign Key

    Can I create a custom table with a foreign key to "umbracoUsers"? And if so how do I decorate the model for the user Id? I created a hack that works for reading the information and presentation but when I try to save my new record I'm failing hard.

        private DatabaseContext dbc = ApplicationContext.Current.DatabaseContext;
    
            private umbUser _creator;
            private umbUser _modifier;
    
            public Features()
            {
            }
    
            [PrimaryKeyColumn(AutoIncrement = true)]
            public int id { get; set; }
    [Column("modified_by")]
            public int modified_by { get; set; } // int
    
            [Column("created_by")]
            public int created_by { get; set; } // int
     // [ForeignKey(typeof(Umbraco.Core.Models.Membership.User), Name = "FK_nftaFeatures_umbracoUser_created", Column = "id")]
            public umbUser creator {
                get {
                    if (created_by >= 0)
                    {
                        var qry = new Sql().Select("*").From(dbc.SqlSyntax.GetQuotedTableName("umbracoUser")).Where<umbUser>(x => x.id == created_by, dbc.SqlSyntax);
                        _creator = dbc.Database.FirstOrDefault<umbUser>(qry);
    
                        return _creator;
                    }
    
                    return _creator;
                }
                set {
                    if(created_by >= 0)
                    {
                        var qry = new Sql().Select("*").From(dbc.SqlSyntax.GetQuotedTableName("umbracoUser")).Where<umbUser>(x => x.id == created_by, dbc.SqlSyntax);
                        _creator = dbc.Database.FirstOrDefault<umbUser>(qry);
                    }
                }
            }
    
            // [ForeignKey(typeof(Umbraco.Core.Models.Membership.User), Name = "FK_nftaFeatures_umbracoUser_modified", Column = "id")]
            public umbUser modifier
            {
                get
                {
                    if (created_by >= 0)
                    {
                        var qry = new Sql().Select("*").From(dbc.SqlSyntax.GetQuotedTableName("umbracoUser")).Where<umbUser>(x => x.id == modified_by, dbc.SqlSyntax);
                        _modifier = dbc.Database.FirstOrDefault<umbUser>(qry);
    
                        return _modifier;
                    }
    
                    return _modifier;
                }
                set
                {
                    if (created_by >= 0)
                    {
                        var qry = new Sql().Select("*").From(dbc.SqlSyntax.GetQuotedTableName("umbracoUser")).Where<umbUser>(x => x.id == modified_by, dbc.SqlSyntax);
                        _modifier = dbc.Database.FirstOrDefault<umbUser>(qry);
                    }
                }
            }
    }
    
    
        [TableName("umbracoUser")]
            public class umbUser {
                public int id { get; set; } // id int Unchecked
                public string userName { get; set; } //  nvarchar(255)   Unchecked
                public string userLogin { get; set; } //    nvarchar(125)   Unchecked
                public string userEmail { get; set; } //    nvarchar(255)   Unchecked
                public string userLanguage { get; set; } //     nvarchar(10)    Checked
            }
    

    bump...

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Apr 15, 2017 @ 08:59
    David Brendel
    0

    Hi Max,

    don't think it is a good idea to fetch an umbraco entity directly by using a foreign key.

    For me, I just saved the users ID as an int value and when returning I first get my stuff from the DB and when used the Umbraco services for fetching the user by the ID present.

    Regards David

Please Sign in or register to post replies

Write your reply to:

Draft