Copied to clipboard

Flag this post as spam?

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


  • André Lange 108 posts 410 karma points
    Nov 21, 2020 @ 15:05
    André Lange
    0

    Umbraco Migration component, datetimeoffset becomes timespan

    So i have set up a migration which seems to work fine, the only issues is that my datetimeoffset becomes a timespan in the database.

    And i cannot seem to figure out why, any help would be usefull.

    Here is my complete code.

    My Schema

    public class ArticleLikeSchema
        {
            [PrimaryKeyColumn(AutoIncrement = true, IdentitySeed = 1)]
            [Column("Id")]
            public int Id { get; set; }
    
            [Column("Liked")]
            public bool Liked { get; set; }
    
            [Column("UmbracoPageId")]
            public int UmbracoPageId { get; set; }
    
            [Column("UserID")]
            public string UserID { get; set; }
    
            [Column("Created")]
            public DateTimeOffset Created { get; set; }
        }
    

    My Migration

    public class AddArticleLikesTable : MigrationBase
        {
            private readonly ILogger logger;
    
            public AddArticleLikesTable(IMigrationContext context, ILogger logger) : base(context)
            {
                this.logger = logger;
            }
            public override void Migrate()
            {
                logger.Debug<AddArticleLikesTable>("Running migration {MigrationStep}", "AddArticleCommentsTable");
    
                // Lots of methods available in the MigrationBase class - discover with this.
                if (TableExists("ArticleLikes") == false)
                {
                    Create.Table<ArticleLikeSchema>().Do();
                }
                else
                {
                    logger.Debug<AddArticleLikesTable>("The database table {DbTable} already exists, skipping", "ArticleComments");
                }
            }
        }
    

    And my Component

    public class ArticleLikeMigrationComponent : IComponent
        {
            private readonly ILogger logger;
            private readonly IScopeProvider scopeProvider;
            private readonly IMigrationBuilder migrationBuilder;
            private readonly IKeyValueService keyValueService;
    
            public ArticleLikeMigrationComponent(ILogger logger, IScopeProvider scopeProvider, IMigrationBuilder migrationBuilder, IKeyValueService keyValueService)
            {
                this.logger = logger;
                this.scopeProvider = scopeProvider;
                this.migrationBuilder = migrationBuilder;
                this.keyValueService = keyValueService;
            }
    
            public void Initialize()
            {
                // Create a migration plan for a specific project/feature
                // We can then track that latest migration state/step for this project/feature
                var likesMigrationPlan = new MigrationPlan("ArticleLikes");
    
                // This is the steps we need to take
                // Each step in the migration adds a unique value
    
                likesMigrationPlan.From(string.Empty)
                    .To<AddArticleLikesTable>("Article-Like-migration");
    
                // Go and upgrade our site (Will check if it needs to do the work or not)
                // Based on the current/latest step
    
                var upgrader2 = new Upgrader(likesMigrationPlan);
                upgrader2.Execute(scopeProvider, migrationBuilder, keyValueService, logger);
            }
    
            public void Terminate()
            {
            }
        }
    
  • Malthe Petersen 68 posts 383 karma points c-trib
    Nov 21, 2020 @ 15:52
    Malthe Petersen
    0

    Hey André,

    Just wondering why you are using DateTimeOffset and not just DateTime? I think for a simple date time, Created = DateTime.Utc.Now should be working perfect for your use case, as I assume that you wish to have a localized date for when something is liked.

    You should be able to localize the date time when rendering the date. Maybe something like this: https://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-sharp

    I haven’t tried it, but I guess it is worth a try.

    Regards Malthe

  • André Lange 108 posts 410 karma points
    Nov 21, 2020 @ 16:06
    André Lange
    0

    Yes i was thinking this as a solution as well, but it is still wierd that it changes it to a timespan. Datetimeoffset should be supported.

Please Sign in or register to post replies

Write your reply to:

Draft