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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:34:50+00:00 2026-06-18T00:34:50+00:00

Possible Duplicate: Cannot add an entity with a key that is already in use

  • 0

Possible Duplicate:
Cannot add an entity with a key that is already in use (LINQ)

I have googled a lot on this issue and found many people facing the same issue. but could get through it yet. many solutions were posted but no gain.

I have written a LINQ query which retrieve some 38 records and using foreach loop i insert those records in other table as per the code.

var Results =  (from p in (from d in compact.CURRENT_STATUS
                                                   where QtrEndDate >= d.ValidStartDate && QtrEndDate <= d.ValidDueDate && d.LocationCode == EmployeeLocation && d.DeptCode == selecteddept
                                            select new
                                            {
                                                loccode = d.LocationCode,
                                                taskcode = d.TaskCode,
                                                deptcode = d.DeptCode,
                                                flag = d.Flag,
                                                startdate = d.ValidStartDate,
                                                duedate = d.ValidDueDate,
                                                remarks = d.Remarks,
                                                content = d.UploadDocContent,
                                                title = d.UploadDocTitle,
                                                type = d.UploadDocType
                                            }).Union(from i in compact.HISTORY_STATUS
                                                     where QtrEndDate >= i.ValidStartDate && QtrEndDate <= i.ValidEndDate && i.LocationCode == EmployeeLocation && i.DeptCode ==  selecteddept
                                                     select new
                                                     {
                                                loccode = i.LocationCode,
                                                taskcode = i.TaskCode,
                                                deptcode = i.DeptCode,
                                                flag = i.Flag,
                                                startdate = i.ValidStartDate,
                                                duedate = i.ValidEndDate.Date,
                                                remarks = i.Remarks,
                                                content = i.UploadDocContent,
                                                title = i.UploadDocTitle,
                                                type = i.UploadDocType
                                                     }) select p);





                        foreach (var item in Results)
                        {
                            REPORT_TABLE RT = new REPORT_TABLE();
                            {

                                RT.LocationCode = item.loccode;
                                RT.TaskCode = item.taskcode;
                                RT.DeptCode = item.deptcode;
                                RT.Flag = item.flag;
                                RT.ValidStartDate = item.startdate;
                                RT.ValidDueDate = item.duedate;
                                RT.Remarks = item.remarks;
                                RT.UploadDocContent = item.content;
                                RT.UploadDocTitle = item.title;
                                RT.UploadDocType = item.type;
                                RT.SubmittedBy = (Session["username"]).ToString();
                                RT.SubmittedByIP = (Session["IP"]).ToString();
                                RT.SubmittedOn = System.DateTime.Now;
                                RT.ReportID = RHqueryFind.ReportID;
                                RT.AsOnDate = QtrEndDate;
                            }
                            compact.REPORT_TABLEs.InsertOnSubmit(RT);  
                            compact.SubmitChanges();
                        };

But i do get the error Cannot add an entity with a key that is already in use in the last line i.e SubmitChanges.

In one of the solution i found a line of code to be added as RefreshMode but this could not solve my problem.

  • 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-18T00:34:51+00:00Added an answer on June 18, 2026 at 12:34 am

    I think you have to remove Primary key of your column. Create new column i.e. SerialNo and set it as primary key with Identity Specification.

    I got same problem i.e. Cannot add an entity with a key that is already in use but when I removed the primary key of column which I was inserting the value and create a new column with Primary key and Identity Specification. Problem solved

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

Sidebar

Related Questions

In SQL its possible to have fields that cannot contain duplicate data. How is
Possible Duplicate: The entity cannot be constructed in a LINQ to Entities query var
Possible Duplicate: Why is this code invalid in C#? Conditional operator cannot cast implicitly?
Possible Duplicate: C# Cannot convert from IEnumerable<Base> to IEnumerable<Derived> I have D1 and D2
Possible Duplicate: add values to enum Why enums in Java cannot inherit from other
Possible Duplicate: PHP error: Cannot modify header information – headers already sent I've built
Possible Duplicate: Cannot use concatenation when declaring default class properties in PHP? The following
possible duplicate: Cannot serialize parameter of type ‘System.Linq.Enumerable… ’ when using WCF, LINQ, JSON
Possible Duplicate: The calling thread cannot access this object because a different thread owns
Possible Duplicate: Decompressing a .gz file via PHP I cannot use gzdecode with php

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.