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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:05:54+00:00 2026-06-01T18:05:54+00:00

I have an entity model with User and Person entities, such that each User

  • 0

I have an entity model with User and Person entities, such that each User must be associated with exactly 1 Person, and each Person can be associated zero or 1 User.

User (0..1) <-------> (1) Person

The association is mapped fluently. Originally I only had it declared on the Person side:

private class PersonOrm : EntityTypeConfiguration<Person>
{
    internal PersonOrm()
    {
        ToTable(typeof(Person).Name, DbSchemaName.People);

        // has zero or one User
        HasOptional(p => p.User)
            .WithRequired(d => d.Person)
            .Map(d => d.MapKey("PersonId"))
            .WillCascadeOnDelete(false)
        ;

Since I encountered this error, I also added the same mapping to the User side:

private class UserOrm : EntityTypeConfiguration<User>
{
    internal UserOrm()
    {
        ToTable(typeof(User).Name, DbSchemaName.Identity);

        // has exactly 1 Person
        HasRequired(p => p.Person)
            .WithOptional(d => d.User)
            .Map(d => d.MapKey("PersonId"))
            .WillCascadeOnDelete(false);

There is a scenario in the application where a new User can be created and associated with an existing Person. This is where I am having difficulty at the moment. EF is considering User as the dependent side of the relationship, and is putting a PersonId (FK, int, not null) column on the User table. I don’t believe it’s possible to use a foreign key property on either entity to help EF manage the association (is it?).

Here is some failing code that tries to handle the scenario:

// find out if Person already exists
var person = context.People.SingleOrDefault(p => p.Emails.Any(
    e => e.Value.Equals(emailAddress, StringComparison.OrdinalIgnoreCase)));

// find out if User already exists
var user = context.Users.SingleOrDefault(
    u => u.Name.Equals(emailAddress, StringComparison.OrdinalIgnoreCase));

if (user == null)
{
    user = new User
    {
        Name = emailAddress,
        IsRegistered = isRegistered,
        Person = person ?? PersonFactory.Create(emailAddress),
        // ignore the PersonFactory.Create, that part works
    };

    context.Entry(user).State = EntityState.Added;
    context.SaveChanges();
}

This code works fine when person is null (does not already exist in the db). However when person is not null (already exists in db) and user is null, the code attempts to create a new User and associate it with the existing Person. When invoking SaveChanges(), I get a DbUpdateException:

An error occurred while saving entities that do not expose foreign key
properties for their relationships. The EntityEntries property will
return null because a single entity cannot be identified as the source
of the exception. Handling of exceptions while saving can be made
easier by exposing foreign key properties in your entity types. See
the InnerException for details.

The inner exception is:

A relationship from the ‘User_Person’ AssociationSet is in the
‘Deleted’ state. Given multiplicity constraints, a corresponding
‘User_Person_Source’ must also in the ‘Deleted’ state.

This doesn’t make any sense to me because I’m not trying to delete anything, and checking the EntityState of both User and Person shows that User is in the Added state, whereas Person is in the Unchanged state. I have overridden SaveChanges() to demonstrate:

public override int SaveChanges()
{
    var userChanges = ChangeTracker.Entries<User>().Single();
    var personChanges = ChangeTracker.Entries<Person>().Single();

    if (userChanges.State == EntityState.Deleted)
        throw new InvalidOperationException("wtf?");

    if (personChanges.State == EntityState.Deleted)
        throw new InvalidOperationException("wtf?");

    return base.SaveChanges();
}

When this code executes, neither InvalidOperationException is thrown. Again, userChanges is in the Added state, and personChanges is in the Unchanged state.

What am I doing wrong?

  • 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-01T18:05:56+00:00Added an answer on June 1, 2026 at 6:05 pm

    I feel really dumb right now. After writing up this careful question, it’s now obvious.

    The Person I am testing with already exists, and already has a User association with a different User.Name. This is why user is coming up null. Setting the Person.User property to a new User is causing the old User to be put in the Deleted state. Doh.

    Sorry to have wasted your time. I’ll leave the question up unless it’s agreed it would be better to delete it.

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

Sidebar

Related Questions

I have a data model that has 2 entities Person and Photo, they have
I have an Entity Data Model that I have created, and its pulling in
I have an Entity Data Model-based app that uses SQL Server CE 3.5 as
In my business model I have Entity class (IPoint interface ) that has a
I have an entity model that has audit information on every table (50+ tables)
I have a Model: @Entity @Table(name = User) public class User implements Serializable {
I have a User entity that has a Current Location field (city and country).
I have a WPF app that updates my database from an in-code entity model.
I have an entity 4.0 model that is using a SqlServerCE database as its
it's possible to have one entity object (class, e.g. User) for more entity models

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.