I'm having issue in Insertion of data in Temporal table
using C# Entity Framework
The Table schema is
CREATE TABLE People(
PeopleID int PRIMARY KEY NOT NULL,
Name varchar(50) Null,
LastName varchar(100) NULL,
NickName varchar(25),
StartTime datetime2 GENERATED ALWAYS AS ROW START NOT NULL,
EndTime datetime2 GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME (StartTime,EndTime)
) WITH (SYSTEM_VERSIONING = ON(HISTORY_TABLE = dbo.PeopleHistory));
I created an EDMX asusal and I tried to Insert a record using following C# Code
using (var db = new DevDBEntities()) {
People1 peo = new People1() {
PeopleID = 1,
Name = "Emma",
LastName = "Watson",
NickName = "ICE"
};
db.Peoples.Add(peo);
db.SaveChanges();
}
I got an exception while on db.SaveChanges()
"Cannot insert an explicit value into a GENERATED ALWAYS column in table 'DevDB.dbo.People'. Use INSERT with a column list to exclude the GENERATED ALWAYS column, or insert a DEFAULT into GENERATED ALWAYS column."
I tried direct insertion using SQL Server using the following Insert Query, its inserting fine.
INSERT INTO [dbo].[People]
([PeopleID]
,[Name]
,[LastName]
,[NickName])
VALUES
(2
,'John'
,'Math'
,'COOL')
Kindly assist me how to insert an record using C# Entity Framework.
StoreGeneratedPattern
forPeopleID
toIdentity
orComputed
(in case of GUID) at EDMX file and implementIDbCommandTreeInterceptor
to remove primary key column insertion attempt in temporal table using string collection. – AssiutStartTime
&EndTime
are autogenerated columns withGENERATED ALWAYS
- setStoreGeneratedPattern
for those columns toIdentity
. Certainly you need to implement additional custom command tree interceptor implementingIDbCommandTreeInterceptor
to handle those autogenerated columns, maybe requiring more explanation more than this comment. – Assiut