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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:42:05+00:00 2026-05-25T00:42:05+00:00

I’m trying to make a basic Address class, that many different other classes can

  • 0

I’m trying to make a basic Address class, that many different other classes can have (a Company can have a billing address and a shipping address, each employee has a home address, etc…). I am doing this model-first in EF4, and I thought I could do a 1-way-navigation, but this does not work. Here is what I have:

The edmx model. Note that I created a 1-1 relation, then deleted the navigation property on Address so that just Companies and Employees reference an Address but the address does not reference its owner.

Entity Framework 4 Model

The generated SQL looks correct, in that it creates all 3 tables, then enforces the Company -> Address and the Employee -> Address relationships, and does not enforce an Address -> Company or Address -> Employee relationship.

-- Creating table 'Addresses'
CREATE TABLE [Addresses] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Street] nvarchar(4000)  NOT NULL,
    [City] nvarchar(4000)  NOT NULL,
    [State] nvarchar(4000)  NOT NULL,
    [Zip] nvarchar(4000)  NOT NULL
);
GO

-- Creating table 'Companies'
CREATE TABLE [Companies] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(4000)  NOT NULL,
    [Address_Id] int  NOT NULL
);
GO

-- Creating table 'Employees'
CREATE TABLE [Employees] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(4000)  NOT NULL,
    [Address_Id] int  NOT NULL
);
GO

-- Creating foreign key on [Address_Id] in table 'Companies'
ALTER TABLE [Companies]
ADD CONSTRAINT [FK_CompanyAddress]
    FOREIGN KEY ([Address_Id])
    REFERENCES [Addresses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating foreign key on [Address_Id] in table 'Employees'
ALTER TABLE [Employees]
ADD CONSTRAINT [FK_EmployeeAddress]
    FOREIGN KEY ([Address_Id])
    REFERENCES [Addresses]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

Now I want to insert a Company and its Address…

    [TestMethod]
    public void TestMethod1()
    {
        var context = new Model1Container();
        context.AddToCompanies(new Company
        {
            Name = "TestCompany",
            Address = new Address
            {
                Street = "TestAddress"
            }
        });
        context.SaveChanges();
    }

This test fails on SaveChanges() with the error:

System.Data.UpdateException: Entities in ‘Model1Container.Addresses’ participate in the ‘EmployeeAddress’ relationship. 0 related ‘Employee’ were found. 1 ‘Employee’ is expected.

So it looks like EF is trying to enforce the non-navigable Address -> Employee relationship on the Company’s Address…

How can I make this work? It seems like this kind of relationship would be fairly common practice… Thanks for any help!

  • 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-25T00:42:06+00:00Added an answer on May 25, 2026 at 12:42 am

    You can’t make it work. The enforcement is not in the database but in your model. You told EF that Address cannot exist without both Company and Employee (those one-to-one relations) – if fails before any SQL is generated. Moreover if you check the generated SQL those relations are not one-to-one but one-to-many because EF doesn’t support this kind of one-to-one relation (it supports it only if dependent entity’s PK is FK to principal entity).

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

Sidebar

Related Questions

No related questions found

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.