views:

556

answers:

1

In my database there's a datetime column "Created" with default value getutcdate(). I would like my EF datacontext to generate an insert query that doesn't set this column, and fetch the resulting value. Is there a way to do this?

I tried setting StoreGeneratedPattern to either None, Identity or Computed, I get an exception that DateTime can't be converted to datetime2 (meaning it's trying to insert 0:00 at 0-0-0)

How do I set up my edmx to allow creating objects without specifying the initial Created value?

A: 

Worked on my machine. Just tried this out, added a property to my model called "Property" ( default name ), set the column to be Nullable: false, StoreGeneratedPattern: computed:

Here is the generated sql for the example I just tried:

insert [dbo].[Foods]
      ([CommonName],
       [ScientificName],
       [ShortDescription],
       [CategoryId],
       [LongDescription])
values('sdfg' /* @0 */,
       'sdfg' /* @1 */,
       'sdfg' /* @2 */,
       1 /* @3 */,
       'sdfg' /* @4 */)
select [Id],
       [Property]
from   [dbo].[Foods]
where  @@ROWCOUNT > 0
       and [Id] = scope_identity()

EF4 even knew how to read the Property property back after inserting.

Edit: Do you have this problem: http://stackoverflow.com/questions/1678474/the-conversion-of-a-datetime2-data-type-to-a-datetime-data-type-resulted-in-an-ou

I knew the exception your getting sounded wrong. You should be getting a different error for having a datetime string fall below the minimum that the sql date format allows. Looks like your mappings are just mapping to the wrong schema.

jfar
I do have the problem described in your Edit: indeed. I'll try Nullable: false, StoreGeneratedPattern: computed once again.
Sander Rijken
@jfar, did you test this using EF1 or using the EF that comes with VS2010/.NET4? I have the property set up as "Nullable: false, StoreGeneratedPattern: computed", and in the DB as NOT NULL and default value/binding getutcdate()
Sander Rijken