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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:44:43+00:00 2026-05-30T20:44:43+00:00

Whenever I do something like the following: public class MyDto { [Key] public int

  • 0

Whenever I do something like the following:

public class MyDto
{
    [Key]
    public int ID { get; set; }

    public int ParentID { get; set; }

    public String Name { get; set; }
}

MyDataContext dataContext = new MyDataContext();

MyParentDto myParentDto; // <-- Existing parent DTO querried from the server. Has a relation to MyDto on MyDto.ParentID == MyParentDto.ID.
List<MyDto> myDtos = new List<MyDto>();

myDtos.Add(new MyDto
    {
        Name = "First MyDto!"
    });

myDtos.Add(new MyDto
    {
        Name = "Second MyDto!"
    });

// Some time later.


foreach (var myDto in myDtos)
{
    myDto.ParentID = myParentDto.ID;
    dataContext.MyDtos.Add(myDto);
}

dataContext.SubmitChanges(OnMyCallback)

I get the following vague exception, but my data submits just fine:

System.ServiceModel.DomainServices.Client.DomainOperationException: Submit operation failed.  An entity with the same identity already exists in this EntitySet.  

The stack trace ends with:

System.ServiceModel.DomainServices.Client.EntitySet.AddToCache(Entity entity)
System.ServiceModel.DomainServices.Client.Entity.AcceptChanges()

Both MyDto instances are set to Detached before they are added to dataContext and New afterwards. If I reduce the number of added MyDto instances to one, I get no error. If I call SubmitChanges in between the two adds. Again, both of the MyDto instances are added to the database just fine, but the client crashes with the Exception. What is going on? Thanks.

Edits:

// On the server

[Insert]
public void InsertMyDto(MyDto a_myDto) // <- Yes I prefix. :p
{
    try
    {
        MyEntity myEntity = new MyDto
        {
            ParentID = a_myDto.ParentID,
            Name = a_myDto.Name
        }   

        ObjectContext.MyEntities.AddObject(myEntity);
        ObjectContext.SaveChanges();

    }
    catch (Exception)
    {
        throw; // <- Never hits this spot.
    }
}

// Call back

public void OnMyCallback(SubmitOperation a_submitOperation)
{
        if (a_submitOperation.HasError)
            throw a_submitOperation.Error; // <- It doesn't matter if I have this or not, the client crashes anyway.

        // Other stuff that is not hit because it throws the exception above.
}
  • 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-30T20:44:44+00:00Added an answer on May 30, 2026 at 8:44 pm

    I found that the solution to my problem is to save the ID back to the dto when the entity is saved. Like this:

    [Insert]
    public void InsertMyDto(MyDto a_myDto) // <- Yes I prefix. :p
    {
        try
        {
            MyEntity myEntity = new MyDto
            {
                ParentID = a_myDto.ParentID,
                Name = a_myDto.Name
            }   
    
            ObjectContext.MyEntities.AddObject(myEntity);
            ObjectContext.SaveChanges();
    
            a_myDto.ID = myEntity.ID; // <- Solution
    
        }
        catch (Exception)
        {
            throw; // <- Never hits this spot.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have something like the following: public class Enclosing<T extends Comparable<T>> { //
whenever i try to run something like the following, firebug tells me that "markers
I need to do something like the following: int main(void) { char a,b,cstring; printf(please
My application features a CMS for editing Django templates. Whenever I type something like
I'm using Ada.Containers.Indefinite_Vectors to implement vectors, but whenever I do something like: size :=
I'm trying to get my mac app to do something whenever Safari is launched.
In C#, Whenever i need to get file path more dyanmically, i do something
My application has something like the following structure window.object1; window.object2; $(document).ready(function() { window.object1 =
I want to implement something like facebook's unread items/notifications. I have the following model
Wondering if it's possible to do something like the following (I know the code

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.