locked
dynamic data registering multiple contexts with EF5 DbFirst RRS feed

  • Question

  • User-1956618660 posted

    Hi

    I'm using dynamic data with DbFirst and EntityFramework 5.0. I need to register 2 databases in the model .

    The code to register a context of a DB that I use in Global.asax is the code suggested by Microsoft in http://go.microsoft.com/fwlink/?LinkId=257395.

    I tried only to copy this code for both the DBs, since in the previous version of EF and with Linq2Sql this was the right way: 

    DefaultModel.RegisterContext(() =>
    {
         return ((IObjectContextAdapter)new My1stDBContextType()).ObjectContext;
    }, new ContextConfiguration() { ScaffoldAllTables = true });
    
    DefaultModel.RegisterContext(() =>
    {
        return ((IObjectContextAdapter)new My2ndDBContextType()).ObjectContext;
    }, new ContextConfiguration() { ScaffoldAllTables = true });

    but it doen't seem to be the right way for EF5 since I get this error:

    Item has already been added. Key in dictionary: 'System.Data.Objects.ObjectContext'  Key being added: 'System.Data.Objects.ObjectContext'

    Is there a way to do this?

    Thanks in advance

    Wednesday, January 2, 2013 9:37 AM

Answers

All replies

  • User-330204900 posted

    Hi Marco, see David Ebbo's sample  here Using Dynamic Data with multiple databases I used this with DD and EF5 recently :)

    Wednesday, January 2, 2013 5:32 PM
  • User-1956618660 posted

    Thanks for your reply.

    I tried to use the code in your link but I get the same error

    Item has already been added. Key in dictionary: 'System.Data.Objects.ObjectContext'  Key being added: 'System.Data.Objects.ObjectContext' 

    In the code in the link there's the code:

    model.RegisterContext(contextType, new ContextConfiguration() { ScaffoldAllTables = true });

    that I have to replace with this code:

    model.RegisterContext(() =>
            {
                return (
                            (System.Data.Entity.Infrastructure.IObjectContextAdapter)new MyContext()
                       ).ObjectContext;
            }, new ContextConfiguration() { ScaffoldAllTables = true });

    When the second database is registered these lines generates the error.




    Thursday, January 3, 2013 6:22 AM
  • User-330204900 posted

    Are you using the new DynamicData..EFCodeFirstProvider or are you deleting the TT files and setting the code generation to Default?

    Friday, January 4, 2013 5:54 AM
  • User-1956618660 posted

    No I'm not using EFCodeFirstProvider. I'm using DB first and the code generation is set to None. At every update of the database I have to run the  "Run Custom Tool". Maybe I'm missing something on the general setup of Dynamic Data with EF5...

    Friday, January 4, 2013 12:38 PM
  • User-330204900 posted

    I always delete the TT files and then set the "Code Generation Strategy" to Default the it all works for me, the alternative is to use the EFCodeFirstProvider see Using Dynamic Data with Entity Framework DbContext post on the .NET Web Development and Tools Blog

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 4, 2013 12:54 PM
  • User-1956618660 posted

    Thanks,

    the right way for me was to delete the TT files and set the "Code Generation Strategy" to Default.

    Using EFCodeFirstProvider instead generates the same error 

    Item has already been added. Key in dictionary: 'System.Data.Objects.ObjectContext'  Key being added: 'System.Data.Objects.ObjectContext'
    Sunday, January 6, 2013 5:16 AM