Copied to clipboard

Flag this post as spam?

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


  • Daniel Gillett 72 posts 149 karma points
    Jun 10, 2020 @ 21:03
    Daniel Gillett
    0

    Help Inserting Into MyTable with Migrations

    Hello,

    I've created a subscribers table using migrationgs (very interesting).

    I want now to add data to myTable ...using Migrations? I've built a class that I thought would do this using the insert.IntoTable("myTable").Row(takes an object).Do() ...but I don't see where I can pass in my data model. Please help ...here's what I did...

    namespace DanDoesWebsites.Core.Migrations
    {
        public class AddSubscriber : MigrationBase
        {
            public AddSubscriber(IMigrationContext context) : base(context)
            {
            }
    
            public override void Migrate()
            {
                Logger.Debug<AddSubscriber>("Adding Subscriber migration {MigrationStep}", "AddSubscriber");
    
                // Lots of methods available in the MigrationBase class - discover with this.
                if(TableExists("Subscribers")  == true)
                {
                    Insert.IntoTable("Subscribers").Row(myModel).Do();
                }
            }
        }
    }
    

    I can't see where I can pass in my data object (myModel). Am I not supposed to use Migrations to insert a row/object into myTable?

    Thanks for your help. I'm really trying to get under the hood of umbraco and get to the next level.

    Many thanks,

    Daniel

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 10, 2020 @ 21:24
    Nik
    0

    Hi Daniel,

    Okay, so the purpose of your migration is normally to create a table/update a table (or multiple). The only time it should be adding data is if you need seed data in the tables in my opinion.

    Once the tables are there for storing the data, the question becomes how are you getting the data you want to store? Is it via a data importer in the back office? Is it via a web hook from an external service? Is it from a form on the front end of your website?

    Each of these would be recieved by some sort of controller which could then pass it in to a custom service that is responsible for writing the data to the custom table.

    Think about it as 2 parts... 1) Migration is setup, 2) Service + Controllers is for data read/write

    Does that help?

    Nik

  • Daniel Gillett 72 posts 149 karma points
    Jun 11, 2020 @ 09:01
    Daniel Gillett
    0

    Hi Nik,

    Thank you very much for coming back to me! The table I created is for a form on my site. This particular one was a subscribe form. Eventually, I will build a section for subscribers and manage them that way, with email sending, etc. But I'm not quite ready for that. lol

    I looked at the NPoco and i'm happy to use it for CRUD operations, however I didn't want to create new database connections if the Umbraco connection is there. ...so I thought migrations was the way to go.

    Would you happen to have an example of adding data to a table via a controller? I've got a controller to handle my form and I'm fairly comfortable with Services. ;-)

    BTW - it was suggested to me to use Umbraco Members and create a new group for subscribers and also to use MailChimp. Is that a smarter way to go? There doesn't seem much by way of examples in the documentation.

    Thanks again for your time. Very much appreciated!

    Best,

    Daniel

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 11, 2020 @ 10:18
    Nik
    0

    Hey Daniel,

    Okay, so this is a very rough bit of sudo code to try and explain a bit further for you (I hope it makes more sense).

    1) Have a migration that creates your empty table in the database

    2) Have a Controller that receives your submissions from the front end

    3) In the controller use something like this to save the data to the database (This could be in a service as well)

    private void SaveToDatabase(SubscriberModel subscriber)
    {
         using (var scope = scopeProvider.CreateScope())
         {                
                scope.Database.BeginTransaction();
                try
                {
                    scope.Database.Insert(subscriber);
                    scope.Database.CompleteTransaction();
    
                }
                catch (Exception)
                {
                    scope.Database.AbortTransaction();
                    throw;
                }
                finally
                {
                    scope.Complete();
                }
    
            }
        }
    
  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 11, 2020 @ 10:19
    Nik
    0

    With regards to the question around MailChimp... personally, yes if that's the use case, I wouldn't want to be responsible for managing that data so I'd always off load it to an established service like that personally.

Please Sign in or register to post replies

Write your reply to:

Draft