Entity Framework code-first error when using stored procedure with fields that have default value

c# entity-framework entity-framework-6 sql-server stored-procedures

Question

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; }
1
0
12/1/2015 5:50:44 AM

Accepted Answer

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()
0
12/17/2015 3:29:42 PM


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