Entity framework do not refreshes ID in entity after insert

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
Vipul.Pujari
Posts: 4
Joined: Mon 04 Jan 2016 13:05

Entity framework do not refreshes ID in entity after insert

Post by Vipul.Pujari » Mon 04 Jan 2016 14:18

Hello

Need help on devart with entity framework 6.0(Fluent API) and oracle enterprise edition release 12.1.0.2.0 - 64bit

In database we are using sequence and trigger to insert id value for column on insert

Code: Select all

CREATE OR REPLACE TRIGGER TEST_TRG
  BEFORE INSERT ON TEST_TABLE
  REFERENCING NEW AS NEW OLD AS OLD
  FOR EACH ROW
BEGIN
  if :new.ID = 0 then
    :new.ID := MYSEQUENCE.nextval;
  end if;
END; 
and in application we are marking id property of entity as Identity using fluent API

Code: Select all

this.Property(t => t.Id).HasColumnName("ID").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
Using above configuration whenever we insert model in database we get following error in application
A null store-generated value was returned for a non-nullable member 'Id' of type Test
However in above config if we change DatabaseGeneratedOption to none, application adds model in database but do not refreshes id property in model. This implies there is no issue with trigger and sequence in database

Please suggest if I am missing any config in entity framework because of which entity framework is not refreshing model property in dbcontext

Thanks

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Entity framework do not refreshes ID in entity after insert

Post by Shalex » Mon 04 Jan 2016 18:13

Please use the DatabaseGeneratedOption.Identity option but replace

Code: Select all

  if :new.ID = 0 then
    :new.ID := MYSEQUENCE.nextval;
  end if;
with

Code: Select all

  :new.ID := MYSEQUENCE.nextval;
Does your code work in this case?

Vipul.Pujari
Posts: 4
Joined: Mon 04 Jan 2016 13:05

Re: Entity framework do not refreshes ID in entity after insert

Post by Vipul.Pujari » Tue 05 Jan 2016 08:13

Yes. It work in this case. Thanks! for help.

Just for knowledge. The Id property of entity is 0 and not nullable so in devart/oracle does 0 is treated as null for identity and hence it do not fall in if condition?

Code: Select all

  if :new.ID = 0 then

  end if;

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Entity framework do not refreshes ID in entity after insert

Post by Shalex » Tue 05 Jan 2016 11:29

Please enable dbMonitor to trace the SQL statements sent to the server. Does this explain the behaviour you are experiencing?

Post Reply