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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:18:48+00:00 2026-06-07T16:18:48+00:00

I have a table named ServiceProviders with static data. The POCO class is follows

  • 0

I have a table named ServiceProviders with static data. The POCO class is follows

public class ServiceProvider
    {
         public virtual int Id
        {
            get;
            set;
        }

        public virtual string Name
        {
            get;
            set;
        }
 }

I have a Client table. Each client needs to have a reference to one of the “existing” service providers.

    public class Client
        {
         public virtual int Id
        {
            get;
            set;
        }    

        public virtual string FirstName
        {
            get;
            set;
        }

        public virtual string LastName
        {
            get;
            set;
        }

        public virtual ServiceProvider ServiceProvider
        {
            get;
            set;
        }
}

I am configuring the client using EF code first fluent API as follows

public ClientConfiguration(): base()
        {
            HasKey(e => e.Id);
            Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            ToTable("dbo.Clients");
            Property(e => e.FirstName).IsRequired().HasMaxLength(256).IsVariableLength().HasColumnType("nvarchar");
            Property(e => e.LastName).IsRequired().HasMaxLength(256).IsVariableLength().HasColumnType("nvarchar");
            HasRequired(e => e.ServiceProvider).WithMany().Map(m => m.MapKey("ServiceProviderId")).WillCascadeOnDelete(false);
}

When I want to insert a new client’s record, I get a reference of an existing ServiceProvider instance and attach it to the Client’s ServiceProvider property

Client client = new Client() { FirstName = "John", LastName = "Doe" };
client.ServiceProvider = serviceproviders[0]; //An existing ServiceProvider instance fetched from DB earlier

When I call SaveChanges, EF inserts a new (duplicate) record in my ServiceProviders table. All I want it to do is to insert a new record only in the Client Table and set the value of ServiceProviderId column to the existing ServiceProvider instance’s Id.

What am I doing wrong?

  • 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-07T16:18:50+00:00Added an answer on June 7, 2026 at 4:18 pm

    You need to attach the existing ServiceProvider to the context you are saving the new Client in. Attach tells EF that the ServiceProvider already exists in the database and prevents an INSERT for that ServiceProvider:

    Client client = new Client() { FirstName = "John", LastName = "Doe" };
    context.ServiceProviders.Attach(serviceproviders[0]);
    client.ServiceProvider = serviceproviders[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table named location , and I want to get some data
Lets say I have table named Place with columns: placeId int not null auto_increment,
I have a table named Template which I set my messages to send them
i have a table named item with four attribute name,code,class,value now i want to
I have a table named categories like this: id int(11) NO PRI NULL auto_increment
I have table named x Here is the sample data in table x Column1_
I have a table named CLASS with the Fields **{BATCH, DEGREE, DEPT, SEM, SECTION,
I have a table named sessions setup like this: ################################# # set # timestamp
--I have a Table named CLASS with the Following Records in it (PK) ClassID
I have table named A with column B defined int not null Primary Key

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.