locked
EF50: How to update a detached entity with associated entity? RRS feed

  • Question

  • Hi,

    I have a little problem, to update a detached entity associating a other entity.

    [TestMethod]
    public void Is_updated_then_no_error_occurs()
    {
        // Arrange
        const string SandName1 = "FS01";
        const string SandName2 = "FS02";
        var sand1 = EntityHelper.CreateSandEntityInDatabase(SandName1);
        var sand2 = EntityHelper.CreateSandEntityInDatabase(SandName2);
        var sandRecipe = EntityHelper.CreateSandRecipeEntityInDatabase(sand1);
        var sandRecipeId = sandRecipe.SandRecipeId;
    
        // Act
        sandRecipe.Sand = sand2;
        using (var container = new PrintDataPreparationContainer(TestConstants.TestConnectionString))
        {
            container.SandSet.Attach(sand2);
            container.SandRecipeSet.Attach(sandRecipe);
            container.Entry(sandRecipe).State = EntityState.Modified;
            container.Entry(sand2).State = EntityState.Modified;
            container.SaveChanges();
        }
    
        sandRecipe = EntityHelper.GetSandRecipeEntityById(sandRecipeId);
    
        // Assert            
        Assert.IsNotNull(sandRecipe);
        Assert.IsNotNull(sandRecipe.Sand);
        Assert.AreEqual(sandRecipe.Sand.Name, SandName2);
    }

    Please be assured, that the EntityHelper methods work as expected. The Problem is, that my sandRecipe still points to sand1 and not as expected to sand2 and therefore the final Assert fails.

    What is it I am doing wrong?

    Thanks for any help!

    Regard
    Rainer

    Thursday, February 7, 2013 3:39 PM

All replies

  • Hi Rainer,

    Try this: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/42b911ed-702b-4d7a-b14a-5bc618aaa42b


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Monday, February 11, 2013 5:56 AM
  • Hi GHost,

    thank you for the link. Unfortunately it does not give me a answer on how to update a entitiy to which a OTHER associated entity was assigned.

    Within the link, "AttachAsModified(..)" is mentioned, but this method seams not to be available with DbContext and/or EF5.0

    Regards
    Rainer


    Monday, February 11, 2013 12:24 PM
  • Hi Rainer,

    When I search AttachasModified on ADO.net blog, it redirect me to: http://msdn.microsoft.com/en-US/data/jj592676 


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Tuesday, February 12, 2013 2:44 AM
  • Hi Ghost,

    Yes, I have been on that link. But there "AttachasModified" does not occure.

    I tried all the other suggested ways to update a entity which has a association to a other entity. Actualy the association is changed to a different entity (sand2), but when I want to update this change it is not reflected in the database. If I again query the entity it still has the assiciation to "sand1".

    Regards
    Rainer

    Tuesday, February 12, 2013 9:28 AM
  • >>Yes, I have been on that link. But there "AttachasModified" does not occure.

    Yes, It means you need to try new way, the attachasmodified way is out of date.


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Wednesday, February 13, 2013 4:50 AM
  • Hi Ghost,

    thanks for your efforts. But if you take a look at the posted code at top of this thread you will see, that I have already used the new way. Unfortunately this new way does not solve my problem.

    Regards
    Rainer

    Wednesday, February 13, 2013 7:43 AM
  • Hi Rainer,

    Sorry, My bad, I will check the code again.

    Have a nice day.


    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Wednesday, February 13, 2013 3:46 PM
  • Hi Rainer,

    Please check whether this works or not:

    using (var container = new PrintDataPreparationContainer(TestConstants.TestConnectionString))
        {
            container.SandSet.Add(sand2);
            container.SandRecipeSet.Add(sandRecipe);
            container.Entry(sandRecipe).State = EntityState.Modified;
            container.Entry(sand2).State = EntityState.Modified;
            container.SaveChanges();
        }
    

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 14, 2013 5:31 AM
  • Hello Alexander,

    thank you for your assistance!

    Your sample code works, but that is not what I am looking for. The question is, how to update detached linked entities. The entities "sand1" and "sand2" are already available in the database and now detached. Here the code of my EntityHelper class doing this:

    public static Sand CreateSandEntityInDatabase(string sandName)
    {
        using (var container = new PrintDataPreparationContainer(TestConstants.TestConnectionString))
        {
            var sand = new Sand { Name = sandName, GranulationMicrometer = 33 };
            container.SandSet.Add(sand);
            container.SaveChanges();
            return sand;
        }
    }

    The "EntityHelper.CreateSandRecipeEntityInDatabase(sand1)" works similar.

    With all the entities in question now being detached, I modify my sandRecipe by assigning sand2 to it instead of sand1. The question is: How can I now update this modified sandRecipe in the database? The code, as it is shown in the begining of does not do this.

    Regards
    Rainer


    Friday, February 15, 2013 6:48 AM