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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:14:33+00:00 2026-05-24T09:14:33+00:00

I have a table Training that has a column CreatedBy which has a referential

  • 0

I have a table “Training” that has a column “CreatedBy” which has a referential integrity to the “User” table. So “CreatedBy” is actually a “User” representing the user who inserted the training record.

Now when I add a new “Training” entity, it’s “CreatedBy” is an old “User”. So it should not try to add the user as well. But it does and so fails because it violates the unique key constraint.

Here’s the code. Where am I going wrong?

    public int AddEntity(Training entity, bool isSaveInstantly)
    {
        this.trainersDeskDBModel.Trainings.AddObject(entity);

        if (isSaveInstantly)
        {
            this.trainersDeskDBModel.SaveChanges();
        }
        return entity.Id;
    }

    public int UpdateEntity(Training entity, bool isSaveInstantly)
    {
        this.trainersDeskDBModel.Trainings.ApplyCurrentValues(entity);
        if (isSaveInstantly)
        {
            this.trainersDeskDBModel.SaveChanges();
        }
        return entity.Id;
    }

    public int Save(Training entity, bool isSavedInstantly)
    {
        IEnumerable<Training> training = this.trainersDeskDBModel.Trainings.Where(x => x.Id == entity.Id);
        if (training != null && training.Count() > 0)
        {
            this.UpdateEntity(entity, isSavedInstantly);
        }
        else
        {
            this.AddEntity(entity, isSavedInstantly);
        }
        return entity.Id;
    }
  • 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-24T09:14:33+00:00Added an answer on May 24, 2026 at 9:14 am

    If you didn’t load the User in the same context where you are adding the new Training in you must EF tell that the User already exists in the DB by attaching it to the context before you add the new Training:

    public int AddEntity(Training entity, bool isSaveInstantly)
    {
        this.trainersDeskDBModel.Users.Attach(entity.CreatedBy);
        this.trainersDeskDBModel.Trainings.AddObject(entity);
    
        if (isSaveInstantly)
        {
            this.trainersDeskDBModel.SaveChanges();
        }
        return entity.Id;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table A in Oracle that has a primary key (id). I need
OK I have a table that has two columns, userID and courseID. It is
We have a website (foo.com) that does online training. A user logs in, then
I have table inside a div tab. The table has 40 rows in it
I have table with some fields that the value will be 1 0. This
i have a table [Company] with a column [Address3] defined as varchar(50) i can
When inserting records to a database table which has a 1:m relationship to another
I have a table/dataset like: 1/1/2009 | Training 1 | Mike 1/1/2009 | Training
I have a MySQL table that I have imported from a CSV file. In
I have a very high-traffic table with a char(50) field which takes part in

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.