Bimal Das
Bimal Das

Reputation: 1992

DateTime property causing new migration update even without any update on model in Entity Framework core

In entity framework, if I run the command "add-migration <name>" in package manager console, each and every time it creates migration with some update commands for the property datetime.

Model:

public class TestDetailBase
    {
        public string TestTitle { get; set; }
        public int TotalTimeinMinutes { get; set; }
        public double PassMarkPercent { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime UpdatedOn { get; set; }
    }

Migration Code:

public partial class newMigration : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.UpdateData(
                table: "TestDetails",
                keyColumn: "TestDetailId",
                keyValue: 1,
                columns: new[] { "CreatedDate", "UpdatedOn" },
                values: new object[] { new DateTime(2020, 3, 2, 8, 16, 33, 900, DateTimeKind.Utc).AddTicks(543), new DateTime(2020, 3, 2, 8, 16, 33, 900, DateTimeKind.Utc).AddTicks(543) });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.UpdateData(
                table: "TestDetails",
                keyColumn: "TestDetailId",
                keyValue: 1,
                columns: new[] { "CreatedDate", "UpdatedOn" },
                values: new object[] { new DateTime(2020, 3, 2, 8, 16, 19, 148, DateTimeKind.Utc).AddTicks(4304), new DateTime(2020, 3, 2, 8, 16, 19, 148, DateTimeKind.Utc).AddTicks(4304) });
        }
    }

I don't want to update the records which already inserted as seed data in previous migration. But If I create new migrations, the above additional migration code is automatically injecting to the migration.

Upvotes: 6

Views: 1198

Answers (2)

Smitha Mohan
Smitha Mohan

Reputation: 1

adding this to HasData is fetching the migration update each time: new DateTime(2023, 4, 17, 9, 57, 33, 970, DateTimeKind.Utc).AddTicks(1250));

Upvotes: 0

Mehrdad
Mehrdad

Reputation: 1733

When your are inserting your Seed's data, Don't use DateTime.Now for initial DateTime Properties. probably in your EntityConfiguration file you are inserting this data with HasData method.

Upvotes: 3

Related Questions