How can I implement DBSet.AddOrUpdate in Entity Framework 4.4?

前端 未结 2 1723
情话喂你
情话喂你 2021-02-19 08:26

In response to Slauma\'s answer to my question about running applications that use EF on Windows XP I am converting my application back from Entity Framework 5.0 to use Entit

相关标签:
2条回答
  • 2021-02-19 08:43

    When you enable migrations for MVC5 web applications, you get the following comment in the Seed method of the configuration:

    //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
    

    My initial stab at this was to user DbSet<MyEntity>.AddOrUpdate(). This will lead to the same error message (and rightly so) as the one raised in this question. The fix is to read the rest of the comment and use the context parameter passed into the Seed function:

        context.MyEntity.AddOrUpdate();
    
    0 讨论(0)
  • 2021-02-19 08:53

    You must add...

    using System.Data.Entity.Migrations;
    

    ...to your code file to have AddOrUpdate available. It is an extension method of IDbSet<T> that is implemented in the IDbSetExtensions class in System.Data.Entity.Migrations namespace.

    0 讨论(0)
提交回复
热议问题