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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:17:29+00:00 2026-06-16T06:17:29+00:00

Trying to accomplish something similiar to http://www.codeproject.com/Articles/206410/How-to-Configure-a-Self-Referencing-Entity-in-Code but in my case I’m not using

  • 0

Trying to accomplish something similiar to http://www.codeproject.com/Articles/206410/How-to-Configure-a-Self-Referencing-Entity-in-Code but in my case I’m not using code first rather db first.
I’m getting this error {“Violation of PRIMARY KEY constraint ‘pk_employee’. Cannot insert duplicate key in object ‘dbo.Employee’.\r\nThe statement has been terminated.”}.

        EmployeeEntity employeeEntity = null;
        EmployeeEntity employeeDelegate = null;

            // already EXISTS in table
            employeeDelegate = new EmployeeEntity
            {
                EMPL_UNO = 1,
                FULLNAME = "manager, name"
            };


        employeeEntity = new EmployeeEntity
        {
            EMPL_UNO = 2,
            FULLNAME = "employee, name",
            DELEGATE_EMPL_UNO = 1,
            Delegate = employeeDelegate
        };



MyContext.EmployeeEntities.Add(Employee);
    // throws error
MyContext.SaveChanges();

// table

    CREATE TABLE [dbo].[Employee](
    [EMPL_UNO] [int] NOT NULL,
    [FULLNAME] [varchar](255) NULL,
    [DELEGATE_EMPL_UNO] [int] NULL,
 CONSTRAINT [pk_employee] PRIMARY KEY CLUSTERED 
(
    [EMPL_UNO] 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

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Employee]  WITH CHECK ADD  CONSTRAINT [FK_Employee_Delegate] FOREIGN KEY([DELEGATE_EMPL_UNO])
REFERENCES [dbo].[Employee] ([EMPL_UNO])
GO

ALTER TABLE [dbo].[Employee] CHECK CONSTRAINT [FK_Employee_Delegate]
GO

// Entity

  public partial class EmployeeEntity
{
    public EmployeeEntity()
    {
        this.SubOrdinates = new HashSet<EmployeeEntity>();
    }

    public int EMPL_UNO { get; set; }
    public string FULLNAME { get; set; }
    public Nullable<int> DELEGATE_EMPL_UNO { get; set; }

    public virtual ICollection<EmployeeEntity> SubOrdinates { get; set; }
    public virtual EmployeeEntity Delegate { get; set; }

}
  • 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-16T06:17:30+00:00Added an answer on June 16, 2026 at 6:17 am

    Your code fails because Add method will insert all unknown entities from the object graph and in your case both new and existing employees are unknown to EF context because you didn’t inform the context about the existence of the first entity (setting the id is not enough). You can for example use:

    var employeeDelegate  = new EmployeeEntity {
        EMPL_UNO = 1,
        FULLNAME = "manager, name"
    };
    
    MyContext.EmployeeEntities.Attach(employeeDelegate);
    
    var employeeEntity = new EmployeeEntity {
        EMPL_UNO = 2,
        FULLNAME = "employee, name",
        DELEGATE_EMPL_UNO = 1,
        Delegate = employeeDelegate
    };
    
    
    MyContext.EmployeeEntities.Add(Employee);
    MyContext.SaveChanges();
    

    But in your particular case this should work as well:

    var employeeEntity = new EmployeeEntity {
        EMPL_UNO = 2,
        FULLNAME = "employee, name",
        DELEGATE_EMPL_UNO = 1 // Just set the FK, you don't need a reference if you don't want to modify the delegate as well
    };
    
    MyContext.EmployeeEntities.Add(Employee);
    MyContext.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to accomplish something similar to what was described in this thread:
I'm trying to accomplish something very, very similar to the below picture with CSS3
Im trying to accomplish something which may seem a bit convoluted but would be
I am trying to accomplish something very simple, and I'm hoping someone can point
I am new to cakephp and I'm trying to accomplish something that should be
Hey all, I am trying to accomplish something very simple yet getting an error
There is something I am trying to accomplish although I'm not really sure where
I'm trying to accomplish something similar to the fellow here , but going with
I've been trying to understand the effect presented on this site - http://artofflightmovie.com/ for
this is what I'm trying to accomplish: creating symbolic link from var/www/html to a

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.