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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:41:43+00:00 2026-06-13T07:41:43+00:00

Using Entity Framework 5 So I have a Customer. The Customer could have many

  • 0

Using Entity Framework 5

So I have a Customer. The Customer could have many Addresses, but at least one. One of the addresses would also be set as the primary address (required). I tried various mappings, but so far I get an error on build or when I seed the database.

Customer:

public class Customer
{
    public int CustomerId { get; set;}
    public String CustomerName { get; set; }
    public int PrimaryAddressId { get; set; }
    public virtual CustomerAddress PrimaryAddress { get; set; }
    public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }    
}

Address:

public class CustomerAddress : Address
{
    public int CustomerAddressId { get; set; }
    public int CustomerId { get; set; }
    public virtual Customer Customer { get; set; }
}

This part of my mapping is working correctly. It is on CustomerAddress.

        this.HasRequired(c => c.Customer)
            .WithMany(d => d.CustomerAddresses)
            .HasForeignKey(c => c.CustomerId);

But how do I specify the correct mapping for setting PrimaryAddress in Customer? Or is it the wrong approach?

Thanks

EDIT – Using both Arnolds and LueTM’s answers:

This code is now working.

Customer:

public class Customer
{
    public int CustomerId { get; set;}
    public String CustomerName { get; set; }
    // public int PrimaryAddressId { get; set; } created in mapping
    public virtual CustomerAddress PrimaryAddress { get; set; }
    public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }    
}

Address:

public class CustomerAddress : Address
{
    public int CustomerAddressId { get; set; }
    public int CustomerId { get; set; }
    public virtual Customer Customer { get; set; }
}

Customer mapping:

        modelBuilder.Entity<Customer>
            .HasOptional(c => c.PrimaryAddress)
            .WithOptionalDependent().Map(m => m.MapKey("PrimaryAddressId"));

        modelBuilder.Entity<Customer>
            .HasMany(c => c.CustomerAddresses)
            .WithRequired(c => c.Customer)
            .HasForeignKey(c => c.CustomerId)
            .WillCascadeOnDelete(false);

And I use a repository to make sure that a new address is created first, saved, and then also set as primary and saved again. The repository makes sure the primary is “required”.

  • 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-13T07:41:44+00:00Added an answer on June 13, 2026 at 7:41 am

    Since you don’t show the exception, I have to assume that you ran into a chicken-egg problem.

    If you set PrimaryAddress as a required property EF must have an existing address Id to establish the foreign key (set PrimaryAddressId in Customer). However, since Address requires a Customer you cannot store an address before its customer. And if you try to save an address and a customer in one take, EF can not determine the correct order of inserts, because it needs to insert both objects with the generated Id of the other object.

    So either Address or Customer must have an optional foreign key.

    I would make Customer.PrimaryAddressId optional:

    modelBuilder.Entity<Customer>().HasOptional(c => c.PrimaryAddress)
        .WithOptionalDependent();
    

    Now you can store addresses and assign a primary address to a customer in separeate transactions. But you need business logic to ensure that a Customer always has a primary address.

    If you want to save cutomers and addresses in one transaction, an approach could be to add an IsPrimary property (bool) to CustomerAddress and ensure that always exactly one address has true.

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

Sidebar

Related Questions

I'm using Entity Framework 4 and have a one-to-many relationship between a parent and
Trying to map the following schema using the Entity Framework. A Customer can have
Let's say I have 'Customer' table in SQL DB and I'm using Entity Framework.
I am using Entity Framework and I have two tables, a contact relationship table
I'm using Entity Framework and I have a custum IQueryProvider. I use the Execute
I am using Entity Framework 5 RC and i have some code that require
I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I have created a new application using Entity Framework 4.3 database migrations. The migrations
I have asp.net application and am using Entity Framework to connect it with the
I am using Entity Framework and DevExpress 10.5 XtraGrid. Imagine that we have entities

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.