0

I am working on an application in which i am using Entity Framewirk 4 and SQL server 2008, i am calling my code from a WCF Rest service. When i add a new record in the database using the service and in the same session querying the database against that record, it returns me all the information except the one that is in nullable computed columns, but if i execute the same query in a new session, it'll return me everything.

Definition for the computed column is

[ComputedColumn] AS (dateadd(minute,[OffsetMinutes],[ActualTimeUTC]))

Kindly enlighten me on this.

Thanks

MegaMind
  • 4,120
  • 13
  • 47
  • 93
  • You cannot *persist* data into a computed column - it's **computed** as its name implies, by SQL Server internally. – marc_s Mar 13 '12 at 05:58
  • But there must be a way of getting that data back, or how could i am getting the same data in another session. – MegaMind Mar 13 '12 at 06:00
  • So "ActualTimeUTC" and "OffsetMinutes" are columns in your database? Are the getting filled by the INSERT from EF ?? – marc_s Mar 13 '12 at 06:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8812/discussion-between-manav-inder-and-marc-s) – MegaMind Mar 13 '12 at 06:15

1 Answers1

1

When i add a new record in the database using the service

If you do this it should already fill computed data for you in current entity instance. You just need to mark the column as computed either in code first:

modelBuilder.Entity<YourEntity>()
            .Propety(e => e.YourComputedProperty)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

or data annotations

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

or in EDMX via StoreGeneratedPattern

in the same session querying the database against that record, it returns me all the information except the one that is in nullable computed columns

It returns you current state known by context because of identity map. EF doesn't really load data from the database again (even it executes the query) unless you force it to do that. It by default uses data already stored in the context - the data you inserted.

To force reload you can for example do:

context.Entry(yourEntityToBeReloaded).Reload();
Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • But how should i suppose to input the value in the computed column, you know "COMPUTED" means sql server has the logic/formula to know what should be inputted in this column. Moreover sometime i just need to input the ID field in the computed column, so isn't there some another way to do that. – MegaMind Mar 13 '12 at 09:59
  • You will not input any value to computed column. You will let EF to load it for you from database after the value is updated together with your passed data. – Ladislav Mrnka Mar 13 '12 at 12:31
  • I am expecting the same thing, but EF is not loading the computed column values and as per your answer EF is returning the Current State known by object. Is Reloading is the only thing that i could use, – MegaMind Mar 14 '12 at 03:53