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

  • Home
  • SEARCH
  • 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 7657111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:56:27+00:00 2026-05-31T12:56:27+00:00

I’m using EF4. I’m adding a series of new entities from a list of

  • 0

I’m using EF4. I’m adding a series of new entities from a list of DTOs, and I’m not saving changes until after all of them are added. I’m wanting to set the IDs of the DTOs to what the new entities’ IDs are. How on earth do I do this? Does EF provide a mechanism for this?

With a single entity I would do this:

public void InsertMyDto(MyDto a_dto)
{
    var newEntity = new MyEntity
        {
            Name = a_dto.Name,
            Type = a_dto.Type.ToString(),
            Price = a_dto.Price
        };

    _dataContext.MyEntities.AddObject(newEntity);
    _dataContext.SaveChanges();

    a_dto.ID = newEntity.ID;
}

This works fine, but what do I do in this case?

public void InsertMyDtos(IEnumerable<MyDto> a_dtos)
{

    foreach (var myDto in a_dtos)
    {
        var newEntity = new MyEntity
            {
                Name = myDto.Name,
                Type = myDto.Type.ToString(),
                Price = myDto.Price
            };

        // Does some validation logic against the database that might fail.

        _dataContext.MyEntities.AddObject(newEntity);
    }

    _dataContext.SaveChanges();

    // ???
}

I want to save all at once, because I have validation work (not shown above) that is done against the database and fails before it gets to SaveChanges, and if it fails I want it to fail as a whole transaction (i.e. rollback).

  • 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-31T12:56:28+00:00Added an answer on May 31, 2026 at 12:56 pm

    I don’t think that EF can help you here. It even can’t help you for a single instance which forces you to write a_dto.ID = newEntity.ID. The counterpart of this code for multiple entites is to keep track of the pairs of dtos and new entities:

    public void InsertMyDtos(IEnumerable<MyDto> a_dtos)
    {
        Dictionary<MyDto, MyEntity> dict = new Dictionary<MyDto, MyEntity>();
        foreach (var myDto in a_dtos)
        {
            var newEntity = new MyEntity
                {
                    Name = myDto.Name,
                    Type = myDto.Type.ToString(),
                    Price = myDto.Price
                };
    
            dict.Add(myDto, newEntity);
    
            // Does some validation logic against the database that might fail.
    
            _dataContext.MyEntities.AddObject(newEntity);
        }
    
        _dataContext.SaveChanges();
    
        foreach (var item in dict)
            item.Key.ID = item.Value.ID; // Key is MyDto, Value is MyEntity
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am currently running into a problem where an element is coming back from

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.