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 6757641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:40:38+00:00 2026-05-26T13:40:38+00:00

I have a PostgreSql 9.04 database that contains a postgres implementation of the ASPNet

  • 0

I have a PostgreSql 9.04 database that contains a postgres implementation of the ASPNet Membership Services database. This implementation lives in the same database as my own tables, but in a different schema called “security”.

For business reasons, I have included the aspnet_Users, aspnet_Membership, and aspnet_Profiles table in my entity model. Below is the DDL for these tables.

CREATE TABLE security.aspnet_users (
    userid UUID NOT NULL,
    applicationname VARCHAR(255) NOT NULL,
    username VARCHAR(255),
    lastactivitydate TIMESTAMP,
    isanonymous INT,
    CONSTRAINT aspnet_users_userid_pk PRIMARY KEY (userid),
    CONSTRAINT aspnet_users_username_pk UNIQUE (username, applicationname)
);

CREATE TABLE security.aspnet_membership (
    userid UUID NOT NULL,
    password VARCHAR(255),
    passwordsalt VARCHAR(255),
    passwordformat INT,
    email VARCHAR(255),
    passwordquestion VARCHAR(255),
    passwordanswer VARCHAR(255),
    comments VARCHAR(255),
    isapproved INT,
    islockedout INT,
    creationdate TIMESTAMP,
    lastlogindate TIMESTAMP,
    lastpasswordchangeddate TIMESTAMP,
    lastlockoutdate TIMESTAMP,
    failedpasswordattemptcount INT,
    failedpasswordattemptstart TIMESTAMP,
    failedpasswordanswercount INT,
    failedpasswordanswerstart TIMESTAMP,

    CONSTRAINT aspnet_membership_userid_pk PRIMARY KEY (userid),
    CONSTRAINT aspnet_membership_userid_ref FOREIGN KEY (userid) REFERENCES security.aspnet_users (userid)
);

CREATE TABLE security.aspnet_profiles (
    userid                      UUID,
    propertynames               BYTEA NOT NULL,
    propertyvaluesstring        BYTEA NOT NULL,
    propertyvaluesbinary        BYTEA NULL,
    lastupdateddate             TIMESTAMP NOT NULL,

    CONSTRAINT aspnet_profiles_userid_ref FOREIGN KEY (userid) REFERENCES security.aspnet_users (userid),
    CONSTRAINT aspnet_profiles_userid_key UNIQUE (userid)
);

Again, these are the only tables from this schema that are in my model.

Now, when I insert a row into these tables, and I call SaveChanges, I get the following error message:

Unable to determine the principal end of the ‘Membership_Users’ relationship. Multiple added entities may have the same primary key.

Below is a sample of code that I’m using to insert a new or update an existing row. This is representative of everything I’m doing regarding updating the database.

public static void SaveUserProfile( CarSystemEntities context, UserProfileData data ) {
    if ( context == null )
        throw new ArgumentNullException( "context", "You must pass a non-null CarSystemEntities instance." );

    if ( data == null )
        throw new ArgumentNullException( "data", "You must pass a non-null UserProfileData instance." );

    try {
        Profile profile = null;

        if ( !context.Profiles.Any( p => p.Userid == data.ID ) ) {
            profile = new CarSystem.Profile{
                LastUpdatedDate      = data.LastUpdatedDate.LocalDateTime,
                PropertyNames        = data.PropertyNames, 
                PropertyValuesBinary = data.PropertyValuesBinary, 
                PropertyValuesString = data.PropertyValuesString,
                Userid               = data.ID
            };

            context.Profiles.AddObject( profile );
        } else {
            profile = QueryUerProfiles( context ).Where( p => p.Userid == data.ID ).Single();

            if ( profile.LastUpdatedDate      != data.LastUpdatedDate )      profile.LastUpdatedDate      = data.LastUpdatedDate.LocalDateTime;
            if ( profile.PropertyNames        != data.PropertyNames )        profile.PropertyNames        = data.PropertyNames;
            if ( profile.PropertyValuesBinary != data.PropertyValuesBinary ) profile.PropertyValuesBinary = data.PropertyValuesBinary;
            if ( profile.PropertyValuesString != data.PropertyValuesString ) profile.PropertyValuesString = data.PropertyValuesString;
        }

    } catch ( Exception ex ) {
        throw new DataAccessException( DataAccessOperation.SaveProfile, FailureReason.DatabaseError, ex );
    }
}

What is the cause of this error? How do I fix this?

Thanks

Tony

P.S.

Upon further investigation, the problem isn’t when inserting a row, it’s when trying to update a row in the same transaction as the one that inserted the row.

Essentially, there’s a queue of objects to be written to the database. Each object is removed from the queue in turn and a method like the one above is called to save it to the proper table.

The first time a specific ID is seen, the method inserts it. That is, the Any check returns false. The code creates the new entity object and calls the AddObject method for the entity set that corresponds to the table. So far so good.

After that, it is assumed that the Any check will return true, indicating that there is a row with the given ID in it already. It is then supposed to update the row. But it appears that the Any check is returning false the second time, too, even though I called AddObject on the Entity set that corresponds to the table on the first call.

Any idea what I am doing wrong?

PPS

I’ve done some more testing with a Sql monitor open. We are using the Devart dotConnect for PostgreSql library and they have a tool you can download called DbMonitor. This allows you to track the SQL that is emitted & executed by the Entity Framework.

It turns out (predictably, as it happens) that the calls to .Any are executed as queries against the database immediately, while all of the inserts are queued up and applied once you call SaveChanges. The Any calls do not seem to take any rows that are pending inserting into account, just what the database calls return. As the rows to be inserted haven’t been inserted yet, this query will always return false until SaveChanges is called.

As a result, my logic as it stands won’t work. When a row that is pending insertion is updated (that is, it appears a second time in the queue), The insert logic would run a second time. Sometimes I’d get an exception indicating a unique or primary key constraint was being violated, other times I’d get the message I originally posted about.

I need all of the inserts and updates to be done in a single transaction, and I need the inserts to happen in the database at the time of the call to AddObject. Yet I still need all of the inserts & updates to rollback if something goes wrong inserting or updating a single row, or if some other error occurs in the C# code.

Anybody have any ideas how I can do this with the Entity Framework?

  • 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-05-26T13:40:38+00:00Added an answer on May 26, 2026 at 1:40 pm

    I’ve managed to resolve this issue the way I want it to work.

    The cause of all of my problems at first was that the entity framework waits until you call SaveChanges to write any changes to the database. But when you execute a .Any() call, the SQL is generated and executed immediately, ignoring any changes that are pending being written to the database. So my code always thought it had to insert new rows even though one was a genuine insert and the other was supposed to be an update.

    Next I tried using a TransactionScope object, calling SaveChanges() after inserting or updating each object. Then I’d call the TransactionScope instance’s Completed() method to “commit” the changes. Well, low & behold, this ends up working exactly the same was waiting to call SaveChanges! Nothing gets written to the database until you call scope.Completed()!

    The solution I found that works is shown in the code below:

    using ( CarSystemEntities context = new CarSystemEntities() ) { 
        if ( context.Connection.State != ConnectionState.Open ) { 
            context.Connection.Open(); 
        } 
    
        DbTransaction transaction = context.Connection.BeginTransaction(); 
    
        try { 
            <Data processing code here> 
    
            transaction.Commit(); 
    
        } catch ( Exception ex ) { 
            transaction.Rollback(); 
    
            <More exception code here as needed> 
        }             
    }
    

    Combined with calling SaveChanges after each context..AddObject or property update, everything works exactly as I need it to work. The INSERT & Update statements are generated & executed when SaveChanges is called. The calls to .Any also take into account any rows that were inserted into the table by a previous iteration. Everything is inside one real database transaction that can be committed or rolled back as a whole.

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

Sidebar

Related Questions

I currently have a Postgres 8.4 database that contains a varchar(10000) column. I'd like
I have a postgres database with a table that contains rows I want to
I have a postgresql database that has this column structure: Author id name Book
I have a movie database (postgreSQL). One of tables contains actors and movie titles.
I Have to get a movie from a PostgreSQL database that matches a given
Background: I have a PostgreSQL (v8.3) database that is heavily optimized for OLTP. I
I have a client that has a PostgreSQL database and he cannot remember the
I currently have an MS Access application that connects to a PostgreSQL database via
I've got two PostgreSQL databases that have been created using the same sql file.
I have an existing PostgreSQL database that I'm using for a new Django site.

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.