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

c# entity-framework entity-framework-core

Question

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.

1
3
3/2/2020 8:40:20 AM

Accepted Answer

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.

1
3/2/2020 8:48:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow