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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:41:51+00:00 2026-06-11T12:41:51+00:00

I am facing a problem getting Entity Framework to save results from yield return

  • 0

I am facing a problem getting Entity Framework to save results from yield return properly. To illustrate the problem I created 2 sets of methods, one returns an entity and the other returns an IEnumerable using yield return. Journal references UserProfile.

Set 1:

public static UserProfile CreateUser()
{
    return new UserProfile() { 
                 UserId = Guid.Parse("60a3987c-0aa6-4a93-  a5d2-68c51122858b"), 
                 UserName = "jason"
               };
}
public static Journal CreateJournal(UserProfile userProfile)
{
    return new Journal() { UserProfile = userProfile };
}

Set 2:

public static IEnumerable<UserProfile> CreateUsers()
{
     yield return new UserProfile() { 
                          UserId = 
                             Guid.Parse("02cd1e9f-5947-4b08-9616-5b4f4033d074"), 
                          UserName = "john"
                      };
}
public static IEnumerable<Journal> CreateJournals(UserProfile userProfile)
{
    yield return new Journal() { UserProfile = userProfile };
}

TestSet1 and TestSet2 save the results from Set1 and Set2 respectively. TestSet1 works but TestSet2 throws an exception of Violation of PRIMARY KEY constraint ‘PK_dbo.UserProfiles’. Another observation – if I initialize a List and return it instead of yield return then it works.

public static void TestSet1()
{
   var u = CreateUser();
   var j = CreateJournal(u);
   _db.UserProfiles.Add(u);
   _db.Journals.Add(j);
   _db.Commit();
}

public static void TestSet2()
{
   var uList = CreateUsers();
   var jList = CreateJournals(uList.ElementAt(0));
   _db.UserProfiles.Add(uList.ElementAt(0));
   _db.Journals.Add(jList.ElementAt(0));
   _db.Commit();
}

What is your view on why yield return in Set2 doesn’t work?
Thanks

  • 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-11T12:41:52+00:00Added an answer on June 11, 2026 at 12:41 pm

    Whats happening is since you’re not actually materializing the list, you’re creating 2 UserProfile objects. You can see this if you put a breakpoint onto this line:

    yield return new UserProfile() { UserId = Guid.Parse("02cd1e9f-5947-4b08-9616-5b4f4033d074"), UserName = "john"};
    

    You’ll notice it gets called twice – so instead of getting the original UserProfile created for the uList, the jList function is getting a new UserProfile object. Now, while they have the same Guid, they’re not technically equal because they’re reference types and they’re not pointing to the same place. The ChangeTracker will then see that they’re not equal and attempt to create 2 objects in the database, one for the UserProfiles.Add() and then a 2nd attached to your jList, with the same Guid, and that’s how you get your error.

    You can fix your code by calling a ToList() on your CreateUsers in your Test2 function, materializing the list into memory so that everything matches up.

    public static void TestSet2()
    {
       var uList = CreateUsers().ToList();
       var jList = CreateJournals(uList.ElementAt(0));
       _db.UserProfiles.Add(uList.ElementAt(0));
       _db.Journals.Add(jList.ElementAt(0));
       _db.Commit();
    }
    

    More info on how the yield keyword works from here, here, and here

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

Sidebar

Related Questions

I'm facing a problem with dates in python. I'm getting different results with mktime,
I have facing problem in listview i am getting data from server and after
Hi guys I am using entity framework, I am facing some problem while checking
I am facing a problem getting the boolean value from one of the columns
Q:-Now a days I am facing problem in getting IP address of android device
I am facing a problem with EF Code First with TPH. I am getting
Hi I am facing a problem in Message dialog, getting Force close my code
Hay.. I am facing a problem, I am getting a string 'strData' which contain
I am facing problem in getting Facebook data I am using Facebook SDK for
I am getting a value through Query string but facing some problem in printing

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.