Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:55:27+00:00 2026-06-06T23:55:27+00:00

I’m working with Entity Framework (Model First approach) against a PostgreSQL 9.1 database. You

  • 0

I’m working with Entity Framework (Model First approach) against a PostgreSQL 9.1 database.

You probably all know that every table has a hidden column called xmin that I would use to help EF determine if the row has changed before performing an update.

I know that the internals of PostgreSQL may change and that this might not be a good idea at all for production code, but I would like to try.

What would be the required steps to manually update the model to include this column for one table and test its behavior ?

TIA.

EDIT 1 : Here is where I’m so far, using the Model First approach with Self Tracking Entities.

The model has been modified with the following :

CSDL : <Property Name="xmin" Type="Decimal" Nullable="false" Precision="15" Scale="0" ConcurrencyMode="Fixed" />
SSDL : <Property Name="xmin" Type="numeric" Nullable="false" Precision="15" Scale="0" StoreGeneratedPattern="Computed" />

The xmin column is effectively retrieved on SELECT, then used during UPDATE :

UPDATE "schema"."mytable" SET "column1"=FALSE WHERE ("pk"=cast(7526 as numeric)) AND ("xmin"=cast(1185877 as numeric))

The problem here is with the cast used on the xmin column. It fails. What I’ve found so far, is that it works with no cast at all. But I’m not sure how to tell EF to use no cast at all in the query for this column.

BTW the xmin column datatype is ‘xid’ which is unknown to the Npgsql ADO.NET provider.

EDIT 2 : This is the provider that generates the SQL text. After looking into the provider code, I can confirm that when using an integer datatype there is no cast. So, I’ve used an integer in my entity to store the value and tried to select and update the entity. It fails with a concurrency exception.

System.Data.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)

the pgsql log follows :

CEST LOG:  instruction : BEGIN; SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
CEST LOG:  instruction : UPDATE "schema"."mytable" SET "column1"=TRUE WHERE ("pk"=cast(7526 as numeric)) AND ("xmin"=1185882)
CEST LOG:  instruction : ROLLBACK

Humm, may be there is problem with retreiving the affected rows because, if the same query is executed by hand, I have one row updated…

EDIT 3 : I’ve enabled the npgsql debug log.
When xmin has not changed I have the following (UPDATE 1):

7/2/2012 12:00:38 PM    12380   Debug   Entering NpgsqlState.ProcessBackendResponses()
7/2/2012 12:00:38 PM    12380   Debug   Entering PGUtil.ReadString()
7/2/2012 12:00:38 PM    12380   Debug   Get NpgsqlEventLog.LogLevel
7/2/2012 12:00:38 PM    12380   Debug   String read: UPDATE 1.

When it has changed I have the following (UPDATE 0):

7/2/2012 1:50:06 PM 12700   Debug   Entering NpgsqlState.ProcessBackendResponses()
7/2/2012 1:50:06 PM 12700   Debug   Entering PGUtil.ReadString()
7/2/2012 1:50:06 PM 12700   Debug   Get NpgsqlEventLog.LogLevel
7/2/2012 1:50:06 PM 12700   Debug   String read: UPDATE 0.

But unfortunately, in both cases I have an OptimisticConcurrencyException…

EDIT 4 : After reviewing the npgsql log, it seems that entity Framework is internally using DbCommand.ExecuteReader(CommandBehavior.SequentialAccess) followed with a reader.Read() to determine how many rows were affected by the UPDATE statement.

I may be wrong but the provider is returning false during ForwardsOnlyDataReader.Read() which might be the source of the problem.

EDIT 5 : This is definitely a provider issue (version 2.0.11.93 and upcoming version 2.0.11.94).

For an INSERT Statement, the provider only support computed columns when the underlying datatype is SERIAL (int) or BIGSERIAL (bigint).

For an UPDATE statement, the provider does not handle the Returning property of a given DbUpdateCommandTree. This means that no computed column is ever returned. You have to manually refresh the entity object after the call to SaveChanges().
Optimistic concurrency is unsupported so far.

I’ll try to implement minimum support in the provider and keep you posted.

EDIT 6 : The Returning property is now handled in my own version of the provider. I’d be happy to share my code. Don’t hesitate to ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:55:28+00:00Added an answer on June 6, 2026 at 11:55 pm

    xmin is quite unlikely to go away any time soon, and can be used to implement a simple form of optimistic concurrency control for updates. Be careful in that internally it is an unsigned 32-bit number, so don’t try to store it in a signed 32-bit field, or it is likely to work for a while and then break badly.

    As long as you map it to an appropriate data type, read it in up front, include it in the WHERE clause of your UPDATE and take appropriate action on a “not found” condition it is pretty straightforward.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.