I need to insert an entity by calling a stored procedure. The table in the database has a column called CreateDateTime
that has a default value of GetDate()
.
I do not let the stored procedure set / update the CreateDateTime
column.
When I call the dbcontext.SaveChanges()
it does indeed call my stored procedure and it inserts a row but then EF throws the following error:
A function mapping specifies a result column 'CreateDateTime' that the result set does not contain.
My DbContext
class has the following:
modelBuilder
.Entity<Test>()
.MapToStoredProcedures(s =>
s.Update(u => u.HasName("TestUpdate"))
.Delete(d => d.HasName("TestDelete"))
.Insert(i => i.HasName("TestInsert")));
The TestInsert
stored procedure is a simple INSERT INTO... SELECT SCOPE_IDENTITY()
On my Test
class I have a property like the following.
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime CreateDateTime { get; set; }
Per @CodeNotFound suggestion.
The return from the TestInsert stored procedure must return the entire result set and not just the SCOPE_IDENTITY()
Not only that but the stored procedure must contain a parameter for each "mapped" field in the entity model.
My last line it the InsertTest stored procedure looks something like this.
SELECT * FROM Test WHERE ID = SCOPE_IDENTITY()