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");
}
}
}
CRUD operation using petapoco
How to alter/modify existing table in StartUp.cs?Please let me know in detail.
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
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.
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
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
Hi Druv,
any news on this issue? Can you share it with the community.
Thanks for your feedback!
/Michaël
Create a "custom table". Don't touch Umbraco tables. If you need create table at startup create a similar class in your project:
Bye
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
Why migrations?
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
is working on a reply...