none
I can't find Part 2 of the article "Why Does Entity Framework Reinsert Existing Objects into My Database?" by Julie Lerman RRS feed

  • Question

  • Hi

    I was trying to find Part 2 of the article at: http://msdn.microsoft.com/en-us/magazine/dn166926.aspx but it seems to be missing. Part 1 covers a one-to-many relationship. I want to find Part 2 because it covers a many-to-many relationship. If anybody knows where it is (I've done a Google) I'd like to know where it is. Or can I find the answer in one of Julie Lerman's books or online videos? I just want to know how to avoid inserting non-unique values into the tables.

    I'm using C# with Entity Framework 6.1, using LINQ to Entities.

    Thanks.


    • Edited by thisman Sunday, July 27, 2014 2:41 PM
    Sunday, July 27, 2014 2:40 PM

Answers

  • Hello,

    So Screencasts and Topics contains a collection type for each other. Do you try with using Add() method as:

    screencast.Topic.Add(topic);

    It would be same even it is many to many.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 1, 2014 6:23 AM
    Moderator

All replies

  • Hello,

    With your link, I cannot find information shows that Julie Lerman would write the part 2 or maybe I do not see this article carefully, from my understanding, Julie Lerman is to explain “Why Does Entity Framework Reinsert Existing Objects into My Database”.

    >> I just want to know how to avoid inserting non-unique values into the tables.

    I think Julie should provide solutions:

    using (var context = new ScreencastContext())
    
    {
    
      context.Screencasts.Add(screencast);
    
      context.Topics.Attach(topic);
    
      screencast.Topic = topic;
    
      context.SaveChanges();
    
    }
    

    Alternatively, in lieu of context.Topics.Attach(topic), you could set the state of the Topic before or after the fact, explicitly setting its state to Unchanged:

    context.Entry(topic).State = EntityState.Unchanged

    For me, what I did for this scenario before is to query the database firstly to see whether the row already exists in database, however, it would affect the performance.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 28, 2014 8:55 AM
    Moderator
  • Thanks Fred. 

    I had trouble with  my version of screencast.Topic = topic; because the intellisense in Visual Studio 2013 Express gave me a red line after the dot. I wound up with the equivalent of:

    using (ScreencastContext context = new ScreencastContext())

    {
      context.Configuration.AutoDetectChangesEnabled = false;
      context.Configuration.ValidateOnSaveEnabled = false;

      context.Screencasts.Add(screencast);
      context.Topics.Add(topic);
      context.Entry(topic).State = EntityState.Unchanged
      context.SaveChanges();

    }

    It didn't like "Attach" so I used "Add". But then I got a System.InvalidOperationException error. I'm thinking I must've made a mistake somewhere.

    Thanks.


    • Edited by thisman Monday, July 28, 2014 1:52 PM
    Monday, July 28, 2014 1:51 PM
  • Hello,

    >> because the intellisense in Visual Studio 2013 Express gave me a red line after the dot

    It seems that there is a compiled error, could you please share it with us?

    >> It didn't like "Attach" so I used "Add".

    What version of Entity Framework are you using? As far as I know, even in the EF 4, the Attach() method could be already used.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, July 29, 2014 8:30 AM
    Moderator
  • Hi

    I'm using EntityFramework 6.1, with C# in Visual Studio 2013 Express on Windows 8.1 (not Windows 8.1 Professional). I'll go back to the code you posted rather than my own as I've obviously made a mistake. Using the equivalent of this code (i.e. without my own names for the entities):

    using (ScreencastContext context = new ScreencastContext())
    
    {
      context.Configuration.AutoDetectChangesEnabled = false;
      context.Configuration.ValidateOnSaveEnabled = false;
    
      context.Screencasts.Add(screencast);
      context.Entry(topic).State = EntityState.Unchanged
      context.Topics.Attach(topic);
      screencast.Topic = topic;
      context.SaveChanges();
     
    }	

    I get the error:

    Error 1 Cannot implicitly convert type 'ScreencastContext.Topic' to 'System.Collections.ObjectModel.ObservableCollection<ScreencastContext.Topic>'

    The above error is associated with the red line under "topic;" in the line:

    screencast.Topic = topic;

    Thanks.








    • Edited by thisman Wednesday, July 30, 2014 7:23 AM
    Wednesday, July 30, 2014 7:17 AM
  • Hello,

    Are Screencasts and Topics have a one to many relationship and the topic is a collection type property in screencasets entity class as:

    Public class Screencasts
    
    {
    
    ...
    
    Public virtual ObservableCollection < Topic> Topics {get;set;}
    
    }
    

    If it is, since the screencast.Topic is a collection type, we should to use its Add method to add new topic entity as:

    screencast.Topic.Add(topic);

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 30, 2014 8:34 AM
    Moderator
  • Sorry. Screencasts and Topics are in a many-to-many relationship. At least that's how I'm trying to code it as.
    • Edited by thisman Friday, August 1, 2014 3:49 AM
    Thursday, July 31, 2014 12:08 PM
  • Hello,

    So Screencasts and Topics contains a collection type for each other. Do you try with using Add() method as:

    screencast.Topic.Add(topic);

    It would be same even it is many to many.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 1, 2014 6:23 AM
    Moderator