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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:23:27+00:00 2026-05-22T01:23:27+00:00

I have a Customer entity which references a collection of Addresses. The complication here

  • 0

I have a Customer entity which references a collection of Addresses. The complication here is that I want to be able to identify a particular address as the default address.

If possible I would like to hold the FK of the default address in the Customer table. This seems more elegant than having a column in the addresses table to identify the default.

I am having difficulty with the fluent API in terms of defining this relationship. When I run the following code I get an exception which says:
“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.”
“Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.”

I created a console app to show the precise problem. In this test app I have a Customer entity, an Address and the flient api configuration.

Any help would be much appreciated:

using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace OneToManyWithDefault
{

    public class Customer
    {
        private ICollection<Address> m_Addresses;

        public Customer()
        {
            Addresses = new List<Address>();
        }

        public int Id { get; set; }
        public string CompanyName { get; set; }
        public virtual ICollection<Address> Addresses
        {
            get
            {
                if (m_Addresses == null)
                {
                    m_Addresses = new List<Address>();
                }
                return m_Addresses;
            }
            set
            {
                m_Addresses = value;
            }
        }
        public Address DefaultAddress { get; set; }
        public int DefaultAddressId { get; set; }

    }

    public class Address
    {
        public int Id { get; set; }
        public string Town { get; set; }
        public Customer Customer { get; set; }
    }

    public class MyContext
        : DbContext
    {
        public DbSet<Customer> Customers { get; set; }

        public MyContext(string connectionString)
            : base(connectionString)
        {

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CustomerConfiguration());
            modelBuilder.Configurations.Add(new AddressConfiguration());
            base.OnModelCreating(modelBuilder);
        }
    }

    public class CustomerConfiguration
        : EntityTypeConfiguration<Customer>
    {
        public CustomerConfiguration()
            : base()
        {
            HasKey(p => p.Id);
            Property(p => p.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();
            Property(p => p.CompanyName)
                .HasColumnName("Name")
                .IsRequired();

            // Configure the mapping for the Default Address (this is likely to be wrong!):
            HasRequired(p => p.DefaultAddress).WithMany()
                .Map(x => x.MapKey("DefaultAddressId"))
                .WillCascadeOnDelete(false);
            HasRequired(p => p.DefaultAddress)
                .WithMany()
                .HasForeignKey(x => x.DefaultAddressId);

            ToTable("Customers");
        }
    }

    public class AddressConfiguration
        : EntityTypeConfiguration<Address>
    {
        public AddressConfiguration()
            : base()
        {
            HasKey(p => p.Id);
            Property(p => p.Id)
                .HasColumnName("Id")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();
            Property(p => p.Town)
                .HasColumnName("Town")
                .IsRequired();

            HasRequired(p => p.Customer)
                .WithMany(c => c.Addresses)
                .Map(x => x.MapKey("CustomerId"));

            ToTable("Addresses");
        }
    }

    class Program
    {
        private const string ConnectionString =
            @"Server=.\sql2005;Database=OneToManyWithDefault;integrated security=SSPI;";

        static void Main(string[] args)
        {
            Customer headOffice = new Customer();
            headOffice.CompanyName = "C1";

            Address address = new Address();
            address.Town = "Colchester";
            headOffice.Addresses.Add(address);

            address = new Address();
            address.Town = "Norwich";
            headOffice.Addresses.Add(address);
            headOffice.DefaultAddress = address;

            MyContext context = new MyContext(ConnectionString);
            context.Customers.Add(headOffice);
            context.SaveChanges();

            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}

Many thanks,

Paul.

  • 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-22T01:23:28+00:00Added an answer on May 22, 2026 at 1:23 am

    I don’t understand what EF is talking there about “not exposed foreign keys” in the exception. I would consider the inner exception as the important part:

    Unable to determine a valid ordering
    for dependent operations. Dependencies
    may exist due to foreign key
    constraints, model requirements, or
    store-generated values.

    I think the problem in your model is that you have a mutual dependency between Customer and Address: An address needs a customer (you have marked it as required in your mapping code) and on the other hand a customer needs an address (the default address is required both due to the non-nullable foreign key and due to your mapping code). So, EF doesn’t know which entity to save first in your example code – the default address or the customer? Both entities need the primary key of the other to be saved with valid FK contraints.

    The easiest solution I can see is to make the default address optional in your model and then save twice (I omit the mappings which work by convention anyway):

    public class Customer
    {
        private ICollection<Address> m_Addresses;
    
        public Customer() { Addresses = new List<Address>(); }
    
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public virtual ICollection<Address> Addresses { get { ... } set { ... } }
        public Address DefaultAddress { get; set; }
        public int? DefaultAddressId { get; set; } // FK for optional relationship
    }
    
    public class Address
    {
        public int Id { get; set; }
        public string Town { get; set; }
        public Customer Customer { get; set; }
    }
    
    // ...
    
    public class CustomerConfiguration : EntityTypeConfiguration<Customer>
    {
        public CustomerConfiguration() : base()
        {
            Property(p => p.CompanyName)
                .HasColumnName("Name")
                .IsRequired();
    
            HasMany(c => c.Addresses)
                .WithRequired(a => a.Customer)
                .Map(x => x.MapKey("CustomerId"));
        }
    }
    
    public class AddressConfiguration : EntityTypeConfiguration<Address>
    {
        public AddressConfiguration() : base()
        {
            Property(p => p.Town)
                .HasColumnName("Town")
                .IsRequired();
        }
    }
    

    And then your program would look like this:

    static void Main(string[] args)
    {
        Customer headOffice = new Customer();
        headOffice.CompanyName = "C1";
    
        Address address = new Address();
        address.Town = "Colchester";
        headOffice.Addresses.Add(address);
    
        address = new Address();
        address.Town = "Norwich";
        headOffice.Addresses.Add(address);
    
        //headOffice.DefaultAddress = address;
        //We don't set the default address here as SaveChanges would throw an
        //exception. But because it is optional now we are allowed to leave it null.
    
        MyContext context = new MyContext(ConnectionString);
        context.Customers.Add(headOffice);
        context.SaveChanges();
    
        headOffice.DefaultAddress = address; // headoffice and address have now PKs
        context.SaveChanges(); // Updates headoffice in the DB with default address
    }
    

    This double SaveChanges is ugly, but I don’t see another way.

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

Sidebar

Related Questions

I have a customer entity. My customer entity has a collection of address value
I have a Customer class that contains a property, MyProperty, which is of a
I have a hibernate entity called customer which contains the information about customer.In my
I have a 'Customer' POCO entity within my Entity Framework 4 project. I want
I have a Customer class defined like this: @Entity public class Customer { //...
I am new to CRM development. I have a Custom Entity customer. This Entity
Trying to map the following schema using the Entity Framework. A Customer can have
I have an Entity Framework data model. Part of the model is a Customer
We have a WCF service that uses Entity Framework to query a SQL database.
I have a customer class which has both PhoneNumber and Email properties. Using DataAnnotations

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.