Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top