0

I've Altered an existing table to make it temporal, Then I updated the model from the database. I encountered with these error, with the two new columns I added to the table:

Error 3004: Problem in mapping fragments starting at line 1388:No mapping specified for properties TbCities.SysStartTime, TbCities.SysEndTime in Set TbCities. An Entity with Key (PK) will not round-trip when: Entity is type [DiERPModel.TbCities] 1389 15 DiERP

This is my query for altering table:

  --Step 1. Adding nullable the columns
  ALTER TABLE Common.TbCities ADD SysStartTime datetime2 NULL  
  GO
  ALTER TABLE Common.TbCities ADD SysEndTime datetime2 NULL  
  GO
  --Step 2 Adding the default constraints
  ALTER TABLE Common.TbCities ADD CONSTRAINT DF_Cities_SysStartTime DEFAULT GETDATE() FOR SysStartTime;
  GO
  ALTER TABLE Common.TbCities ADD CONSTRAINT DF_Cities_SysEndTime DEFAULT CAST('9999-12-31 23:59:59.9999999' AS DATETIME2) FOR SysEndTime;
  --Step 3 Updating the column
  UPDATE Common.TbCities 
    SET SysStartTime = '19000101 00:00:00.0000000'
        ,SysEndTime = '99991231 23:59:59.9999999'
  GO
  --Step 4 Setting NOT NULL to the columns
  ALTER TABLE Common.TbCities ALTER COLUMN SysStartTime datetime2 NOT NULL  
  GO
  ALTER TABLE Common.TbCities ALTER COLUMN SysEndTime datetime2 NOT NULL  
  GO
  --Step 5 Adding PERIOD FOR SYSTEM_TIME option
  ALTER TABLE Common.TbCities ADD PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime)
  GO
  --Step 6 Setting SYSTEM_VERSIONING property
  ALTER TABLE Common.TbCities
      SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = Common.TbCities_History))
  GO 
mshwf
  • 7,009
  • 12
  • 59
  • 133

1 Answers1

0

I suggest you add:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

to SysStartTime and SysEndTime in your Entity Model Class for TbCities, so that the Entity Framework will know their values will be generated by the SQL server and not set them to its own default value.

KroegerBa
  • 1
  • 3