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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:36:52+00:00 2026-05-18T02:36:52+00:00

I have already read Entity Framework One-To-One Mapping Issues and this is not duplicate

  • 0

I have already read Entity Framework One-To-One Mapping Issues and this is not duplicate as the business rule specs are different here.

There are two tables, Invoices and Orders.

Invoices
-> InvoiceID (Primary, Auto Number)
Orders
-> OrderID (Primary, Auto Number)
-> InvoiceID (FK InvoiceID of Invoices Table)

Now the problem is, EF requires One to Many relationship for this association if names of properties are not same. If names of properties are same then it serves purpose of derived class, but here Order is not derived class or Invoice.

InvoiceID(s) are generated for every shopping cart, but OrderID(s) are only generated for paid invoices, so Every Order has InvoiceID but every Order does not have corresponding Invoice.

If I create a seperate table for this, then I have to write too much code to do it. Is there any way I can remove this restriction and let EF still process my model.

However, currently if I change the model as follow, it works

Invoices
-> InvoiceID (Primary, Auto Number)
Orders
-> OrderID (Auto Number)
-> InvoiceID (Primary, FK InvoiceID of Invoices Table)

But is this good practice? Because by definition InvoiceID of Orders table will certainly be unique, but we will be referring everywhere OrderID for comparison and lot of other references. I know I can index the property, but I dont feel this design is perfect.

One to One Error

  • 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-18T02:36:52+00:00Added an answer on May 18, 2026 at 2:36 am

    What seems to be the obvious solution here is to change the 1:* association between Invoice
    and Order in the EDM into a 1:1 association. However, as you experienced, the mapping will not
    validate when you have a Foreign Key Association between the two entities as in your model.

    The only way to map a unique foreign key association is by using an Independent Association. This is the same type of association that we had in EF3.5, where foreign keys were not supported.

    To turn the foreign key association into an independent association would mean removing the InvoiceID foreign key from the Order entity and recreating the association through mappings.

    To make the change to the association, you’ll need to do the following:

    1. Delete the InvoiceID foreign key property from Order entity.
    2. Select the Asscoation between Invoice and Order.
    3. In the Properties window for the association, open the Referential Constraints by
      clicking the ellipses next to that property.
    4. Delete the constraint by clicking the Delete button.
    5. Right-click the association in the Designer and select Table Mapping from the context menu.
    6. In the Mapping Details window, click the element to expose the drop-down.
    7. From the drop-down, select Order. The mappings should populate automatically.
    8. Return to the Properties window for the association.
    9. For the property called “End2 Multiplicity,” which currently has the value * Collection of Orders, change that property to 1 (One of Order) using its drop-down list.
    10. Validate the model by right-clicking the design surface and choosing Validate. You will see that the error message related to this mapping is gone.

    When encountering this problem in your application, you’ll have to decide which is more important to your model and your application logic: the foreign key scalar (e.g., Order.InvoiceID) or being able to define a 1:1 association between one entity (Invoice) and another (Order) when they are joined through a foreign key (InvoiceID).

    The good news is that the new EF4.0 Lazy Loading will be still working with Independent Associations, just the Foreign key is not exposed. To get that you would have to go over to the navigation property (Invoice) and read its InvoiceID like the code below:

    Order order = context.Orders.First();
    int invoiceID = order.Invoice.InvoiceID;
    

    Or you can use the code below to read it right on the Order entity withought having to Lazy Load or Eager Load the Invoice property:

    int invoiceID = order.InvoiceReference.EntityKey.EntityKeyValues[0].Value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XmlDocument that already exists and is read from a file. I
I have already googled for this I have a Table with following structure in
I believe several of us have already worked on a project where not only
Recently I've read article The Entity Framework In Layered Architecture and there is written
I have already posted something similar here but I would like to ask the
I have already tried PreRenderComplete and unload is too late
The basics have already been answered here . But is there a pre-built PHP
I know I have already answered a similar question ( Running Batch File in
I have a repository which I have already cloned from Subversion. I've been doing
In .NET I have already tried the configuration below successfully: <system.net> <connectionManagement> <add address=*

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.