Copied to clipboard

Flag this post as spam?

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


  • Tom Engan 430 posts 1173 karma points
    Jun 12, 2017 @ 12:36
    Tom Engan
    0

    How to connect a PetaPoco table to a member?

    I want to connect a Destinations table, a one-to-many table to a member, and I use PetaPoco from this example: http://www.computermagic.gr/tutorials/umbraco-7/custom-tables-with-petapoco/the-models/ (like AuthorID in the Book class example).

    Extract from the model: Field nodeId in the Destinations table to be connected to the member:

    [Column ("nodeId")]
    [ForeignKey (typeof (IMember), Name = "FK_HikingDestinations_cmsMember_nodeId")] // allow us to define which class is going to be used to create the foreign key relationship and we also supply the name of the relationship
    [IndexAttribute (IndexTypes.NonClustered, Name = "IX_HikingDestinations_nodeId")] // provide information about the index that we want to use.
    Public int nodeId {get; set; }
    

    Should int nodeId be linked directly to table cmsMember, class Member or class IMember? Anybody who know how this should be done?

    Note: I've used SQL CE in this site for a while.

  • Tom Engan 430 posts 1173 karma points
    Jun 14, 2017 @ 08:50
    Tom Engan
    0

    I've used SQLLite/SQL Server Compact Toolbox to create a table with constraints to cmsMember.

    enter image description here

    with these SQL-commands:

    ALTER TABLE [HikingDestinations] ADD CONSTRAINT [FK_HikingDestinations_cmsMember_nodeId] FOREIGN KEY ([nodeId]) REFERENCES [cmsMember]([nodeId]) ON DELETE NO ACTION ON UPDATE NO ACTION;
    GO
    
    CREATE INDEX [IX_HikingDestinations_nodeId] ON [HikingDestinations] ([nodeId] ASC);
    GO
    

    but how can these be done with the use of modelclass instead of typeof(IMember) (see [ForeignKey (typeof (IMember), Name = "FK_HikingDestinations_cmsMember_nodeId")]) above ?

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jun 14, 2017 @ 08:58
    David Brendel
    0

    Hi Tom,

    also suggesting saving the member id in the table and then fetch the member by using the member service.

    Thus you can rely on that everything is wired up correctly in the member class. Don't think it's a good idea to auto get the member as IMember from a ForeigneKey restraint directly. Also not sure if that is possible at all.

    But if you get it working I would glad to hear how.

    Regards David

  • Tom Engan 430 posts 1173 karma points
    Jun 14, 2017 @ 09:11
    Tom Engan
    0

    Not sure what you mean. Can't find any member id, only nodeId. I've now registered 3 members, and table cmsMember has only 3 records, and columns are: nodeId, Email, LoginName and Password side-by-side (password is encrypted). Thus, nodeId is the same as the member id?

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jun 14, 2017 @ 09:17
    David Brendel
    0

    Hi Tom,

    sorry was a bit misleading. Meant that I always suggest to save the member id( not also).

    As when fetching the member through MemberService you would also get the complete member stuff.

    So save the member id as a "foreign key" without any constraints. Then get your stuff from DB and then fetch the member by the member id present.

    Hope that makes it more clear what I meant.

    Regards David

  • Tom Engan 430 posts 1173 karma points
    Jun 14, 2017 @ 09:28
    Tom Engan
    0

    So then I should start with the model and remove [ForeignKey] and [IndexAttribute] attributes..?

    If I understand you correctly, you mean that table HikingDestination doesn't require constraints, but can stand outside of the standard umbracodatabase, and I can insert member.id in the controller as usual?

    Only advantage that disappears when constraints not added, is loss of the opportunity to direct update and deletion in table [HikingDestinations] (child table to cmsMember), but this is functionality that can be added to controls anyway, I think.

    [HttpPost]
    public ActionResult CreateHikingDestination(HikingDestinationViewModel model)
    {
        IMember member = Services.MemberService.GetByEmail(this.Members.CurrentUserName);
    
        var HikingDestinationAdd = new HikingDestinationViewModel();
    
        //member.id has the same value as field nodeId in db table cmsMember
        HikingDestinationAdd.nodeId = member.Id; 
        HikingDestinationAdd.SelectedHikingDestination = model.SelectedHikingDestination;
        HikingDestinationAdd.Title = model.Title;
        HikingDestinationAdd.StartDate = model.StartDate.Date;
        HikingDestinationAdd.HikingCode = model.HikingCode;
    
        var db = ApplicationContext.DatabaseContext.Database;
        db.Insert(HikingDestinationAdd);
        return RedirectToCurrentUmbracoPage();
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft