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

  • Home
  • SEARCH
  • 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 7751223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:29:33+00:00 2026-06-01T11:29:33+00:00

I’ve been coding a very sizable project and I have run into trouble with

  • 0

I’ve been coding a very sizable project and I have run into trouble with EF4 and joiner table operations.

Suppose we have the following SQL table defintions:

CREATE TABLE [dbo].[SQLEntity](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Field1] [nvarchar](128) NOT NULL,
    [Field2] [nvarchar] (256),
    [DateAdded] [datetime] NOT NULL,
    CONSTRAINT [PK_SQLEntity] PRIMARY KEY CLUSTERED ( 
        [Id] ASC 
    ) WITH (
        PAD_INDEX = OFF, 
        STATISTICS_NORECOMPUTE = OFF, 
        IGNORE_DUP_KEY = OFF, 
        ALLOW_ROW_LOCKS = ON, 
        ALLOW_PAGE_LOCKS = ON
    ) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

CREATE TABLE [dbo].[Util_LookupValues](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Description] [nvarchar](64) NOT NULL,
    CONSTRAINT [PK_Util_LookupValues] PRIMARY KEY CLUSTERED (
        [Id] ASC
    )WITH (
        PAD_INDEX = OFF, 
        STATISTICS_NORECOMPUTE  = OFF, 
        IGNORE_DUP_KEY = OFF, 
        ALLOW_ROW_LOCKS = ON, 
        ALLOW_PAGE_LOCKS  = ON
    ) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Xref_EntityValues](
    [SQLEntityId] [bigint] NOT NULL,
    [LookupId] [int] NOT NULL,
    CONSTRAINT [PK_Xref_PositionBenefits] PRIMARY KEY CLUSTERED (
        [SQLEntityId] ASC,
        [LookupId] ASC
    )WITH (
        PAD_INDEX = OFF, 
        STATISTICS_NORECOMPUTE = OFF, 
        IGNORE_DUP_KEY = OFF, 
        ALLOW_ROW_LOCKS = ON, 
        ALLOW_PAGE_LOCKS = ON
    ) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Xref_EntityValues] WITH CHECK ADD CONSTRAINT [FK_Xref_EntityValues_Entity] FOREIGN KEY([SQLEntityId])
REFERENCES [dbo].[SQLEntity] ([Id])
GO

ALTER TABLE [dbo].[Xref_EntityValues] CHECK CONSTRAINT [FK_Xref_EntityValues_Entity]
GO

ALTER TABLE [dbo].[Xref_EntityValues]  WITH CHECK ADD  CONSTRAINT [FK_Xref_EntityValues_Util_LookupValues] FOREIGN KEY([LookupId])
REFERENCES [dbo].[Util_LookupValues] ([Id])
GO

ALTER TABLE [dbo].[Xref_EntityValues] CHECK CONSTRAINT [FK_Xref_EntityValues_Util_LookupValues]
GO

Once a domain model is crated based on these tables you’d end up with two Entities:
SqlEntity and Util_LookupValues.

At this point that the Util_LookupValues is a table whose values have been defined for lookups only! the Xref_EntityValues is a joiner table that would tie together the Entity object to the lookup value, allowing us to have a meny to meny relationship between the two, preserving the “lookup” functionality for the lookup table.

Util_LookupValues contents

Id   Description
---  ------------
1    Person
2    Car

If no changes are added to the domain model (Lets for the purpose of this question call it DataEntities) then tying up a SQLEntity with Util_LookupValues objects whose PK is 1 and 2 would be done as follows:

IEnumerable<Util_LookupValues> lookupValues = DataEntities.Util_LookupValues.Where( lv => lv.Id == 1 || lv.Id == 2);

SQLEntity entity = new SQLEntity();
entity.Field1 = "some field";
entity.Field2 = "another field";
entity.DateAdded = DateTime.Now;

foreach(Util_LookupValues val in lookupValues)
{
    entity.Util_LookupValues.Add(val);     
}

DataEntities.SQLEntities.Add(entity);
DataEntities.SaveChanges();

Problem though is that instead of just adding values to the Xref_EntityValues, this code also adds a copy of 1 and 2 to the Util_LookupValues with new keys!
The resulting database is as follows:

SQL Entity:
Id  Field1       Field2         DateAdded
-- -------      ------         ----------
1   some field  another field  04/04/2012

Xref_EntityValues:
SQLEntityId   LookupId
-----------   ---------
1              3
1              4

and Util_LookupValues:
Id   Description
---  ------------
1    Person
2    Car
3    Person
4    Car

How do I make it that Util_LookupValues only has the 2 original records and the Xref_EntityValues has the proper foreign keys?

Xref_EntityValues:
SQLEntityId   LookupId
-----------   ---------
1              1
1              2

Util_LookupValues:
Id   Description
---  ------------
1    Person
2    Car
  • 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-01T11:29:35+00:00Added an answer on June 1, 2026 at 11:29 am

    Does this line…

    IEnumerable<Util_LookupValues> lookupValues = DataEntities.Util_LookupValues
        .Where( lv => lv.Id == 1 || lv.Id == 2);
    

    …and this line…

    DataEntities.SQLEntities.Add(entity);
    

    … use the same instance of the DataEntities context? Your code snippet indicates that but perhaps it’s “pseudo code”.

    If the instances are different you get the duplication of entities, yes.

    So, you have three options:

    • Make sure the context instances are the same.
    • Attach the returned Util_LookupValues to the second context:

      foreach(Util_LookupValues val in lookupValues)
      {
          DataEntities.Util_LookupValues.Attach(val);
          entity.Util_LookupValues.Add(val);     
      }
      
    • Don’t perform the first query at all. Instead create “stub” entities and attach them:

          var val = new Util_LookupValues { Id = 1 };
          DataEntities.Util_LookupValues.Attach(val);
          entity.Util_LookupValues.Add(val);     
      
          val = new Util_LookupValues { Id = 2 };
          DataEntities.Util_LookupValues.Attach(val);
          entity.Util_LookupValues.Add(val);     
      

      This work because EF only needs to know the primary key property value when you attach an object to the context to establish a new relationship.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
Specifically, suppose I start with the string string =hello \'i am \' me And
I would like to run a str_replace or preg_replace which looks for certain words
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.