Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 06, 2013 09:16 AM by marco_ked2
Member
7 Points
14 Posts
Jan 02, 2013 01:37 PM|LINK
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
All-Star
27308 Points
5458 Posts
MVP
Jan 02, 2013 09:32 PM|LINK
Hi Marco, see David Ebbo's sample here Using Dynamic Data with multiple databases I used this with DD and EF5 recently :)
Jan 03, 2013 10:22 AM|LINK
Thanks for your reply.
I tried to use the code in your link but I get the same error
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.
Jan 04, 2013 09:54 AM|LINK
Are you using the new DynamicData..EFCodeFirstProvider or are you deleting the TT files and setting the code generation to Default?
Jan 04, 2013 04:38 PM|LINK
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...
Jan 04, 2013 04:54 PM|LINK
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
Jan 06, 2013 09:16 AM|LINK
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
marco_ked2
Member
7 Points
14 Posts
dynamic data registering multiple contexts with EF5 DbFirst
Jan 02, 2013 01:37 PM|LINK
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:
Is there a way to do this?
Thanks in advance
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: dynamic data registering multiple contexts with EF5 DbFirst
Jan 02, 2013 09:32 PM|LINK
Hi Marco, see David Ebbo's sample here Using Dynamic Data with multiple databases I used this with DD and EF5 recently :)
Always seeking an elegant solution.
marco_ked2
Member
7 Points
14 Posts
Re: dynamic data registering multiple contexts with EF5 DbFirst
Jan 03, 2013 10:22 AM|LINK
Thanks for your reply.
I tried to use the code in your link but I get the same error
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.
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: dynamic data registering multiple contexts with EF5 DbFirst
Jan 04, 2013 09:54 AM|LINK
Are you using the new DynamicData..EFCodeFirstProvider or are you deleting the TT files and setting the code generation to Default?
Always seeking an elegant solution.
marco_ked2
Member
7 Points
14 Posts
Re: dynamic data registering multiple contexts with EF5 DbFirst
Jan 04, 2013 04:38 PM|LINK
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...
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: dynamic data registering multiple contexts with EF5 DbFirst
Jan 04, 2013 04:54 PM|LINK
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
Always seeking an elegant solution.
marco_ked2
Member
7 Points
14 Posts
Re: dynamic data registering multiple contexts with EF5 DbFirst
Jan 06, 2013 09:16 AM|LINK
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
<div></div>