Copied to clipboard

Flag this post as spam?

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


  • Dhruv Patel 4 posts 74 karma points
    Apr 05, 2018 @ 12:27
    Dhruv Patel
    0

    CRUD operation using petapoco

    How to alter/modify existing table in StartUp.cs?Please let me know in detail.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 05, 2018 @ 12:37
    Michaël Vanbrabandt
    0

    Hi Dhruv,

    first of all welcome to the Umbraco community!

    Could you explain more in detail what you are trying to do? Which existing table do you which to modify and why?

    Thanks!

    /Michaël

  • Dhruv Patel 4 posts 74 karma points
    Apr 06, 2018 @ 04:27
    Dhruv Patel
    0

    I wants to modify custom table which I have created.For e.g I wants to add more columns and wants to change datatype of existing columns.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 06, 2018 @ 07:03
    Michaël Vanbrabandt
    0

    Hi Dhruv,

    thank you for posting back with more details.

    For your question, follow the link that @Dave has posted here in this thread about Migrations in Umbraco.

    This will allow you to apply changes to existing tables using code.

    Hope this helps.

    /Michaël

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Apr 05, 2018 @ 15:32
    Dave Woestenborghs
    0

    Hi,

    You can use migrations. See this blog for more information :

    https://cultiv.nl/blog/using-umbraco-migrations-to-deploy-changes/

    What table are you trying to modify ? Is it a custom table or Umbraco table.

    If it's a Umbraco table I would urge you not to do this.

    Dave

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 10, 2018 @ 13:11
    Michaël Vanbrabandt
    0

    Hi Druv,

    any news on this issue? Can you share it with the community.

    Thanks for your feedback!

    /Michaël

  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Apr 10, 2018 @ 13:32
    Biagio Paruolo
    0

    Create a "custom table". Don't touch Umbraco tables. If you need create table at startup create a similar class in your project:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Persistence;
    
    
    
    namespace XBusiness.EventHandlers
    {
        public class XUmbracoStartup : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbraco, ApplicationContext context)
            {
                LogHelper.Info(this.GetType(), "XM ApplicationStarted");
    
                var ctx = context.DatabaseContext;
    
                DatabaseSchemaHelper db = new DatabaseSchemaHelper(ctx.Database, context.ProfilingLogger.Logger, ctx.SqlSyntax);
    
               if (!db.TableExist(SettingsTablesNames.RedCNPPraticaTableName))
                {
                    // Create DB table - and set overwrite to false
                    db.CreateTable<U4HDRepo.Pratica>(false);
                }
    
    
    
            }
    
            protected virtual void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                LogHelper.Info(this.GetType(), "XM ApplicationStarting");
    
            }
    
    
        }
    }
    

    Bye

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Apr 10, 2018 @ 13:42
    Dave Woestenborghs
    0

    Hey Biagio,

    I don't think this is the best way to create custom tables. It's better to use migrations. This is how umbraco handles it.

    https://cultiv.nl/blog/using-umbraco-migrations-to-deploy-changes/

    Dave

  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Apr 10, 2018 @ 15:24
    Biagio Paruolo
    0

    Why migrations?

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Apr 10, 2018 @ 15:31
    Dave Woestenborghs
    0

    Because they only run once. Your code will run on every time the application starts.

    Also changing tables like, eg adding a column, become a pain in the ass. Using this code. Not even talking about indexes, constraints etc..

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft