I have the following issue. I use Dynamic Data with Entity Framework 5. I have multiple databases with each database having one ObjectContext. If I use for every ObjectContexts its own web project, everything runs fine. But, I am not able to register all
ObjectContexts at once.
Doing it the direct way by registering the DbContext, I always get the exception "The context type 'MyContext' is not supported.".
For this, I am using the indirect way, to register the ObjectContext: metaModel.RegisterContext(() => { return ((IObjectContextAdapter)MyContext()).ObjectContext; }, ...);
This works as expected. But when I use this pattern more than once for various different Contexts, I get a "Duplicated Key" exception. It seems that the MetaModel uses the type name of ObjectContext as key in an internal dictionary.
On my search I found the DynamicData.EFCodeFirstProvider (see
http://blog.davidebbo.com/2011/01/using-dynamic-data-with-ef-code-first.html). This should solve my problem. But when I use it, I get the same error again "The context type 'DynamicData.EFCodeFirstProvider.EFCodeFirstDataModelProvider' is not supported".
Hello BetsyASP! I was trying to do this. But when it did not work, I got in contact with sjnaughton and he confirmed me, that it does not work anymore with EF 5 and 6.
I add a code sample, you can easily reproduce the problem. I can start the project when one (the first or the second) MetaModel is commented. Both dont work. The form Steve did it, does not work, I get an exception. See the commented code
//model.RegisterContext(typeof(DataModelContext),
// new ContextConfiguration() { ScaffoldAllTables = true });
Add following to global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
// Model1 ======================================================
MetaModel model = new MetaModel();
//model.RegisterContext(typeof(DataModelContext),
// new ContextConfiguration() { ScaffoldAllTables = true });
model.RegisterContext(() =>
{
return ((IObjectContextAdapter)new DataModelContext()).ObjectContext;
}, new ContextConfiguration() { ScaffoldAllTables = true });
routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
{
Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
Model = model
});
// Model2 ======================================================
MetaModel model1 = new MetaModel();
model1.RegisterContext(() =>
{
return ((IObjectContextAdapter)new DataModel2Context()).ObjectContext;
}, new ContextConfiguration() { ScaffoldAllTables = true });
routes.Add(new DynamicDataRoute("Model1/{table}/{action}.aspx")
{
Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
Model = model1
});
}
Add two dummy contexts, as e.g. the followings:
namespace WebApplication1
{
public class DataModel1
{
[Key]
public int Id { get; set; }
public string Data { get; set; }
}
public class DataModelContext : DbContext
{
public DataModelContext()
: base("DataContextConnectionString")
{
}
public DbSet<DataModel1> DataModel { get; set; }
}
public class DataModel2
{
[Key]
public int Id { get; set; }
public string Data { get; set; }
}
public class DataModel2Context : DbContext
{
public DataModel2Context()
: base("DataContext2ConnectionString")
{
}
public DbSet<DataModel2> DataModel { get; set; }
}
}
In web.Config add two connection strings and create two dummy/empty database files:
>>Now we need to register our context with Dynamic Data, which is the part that requires special handling.
>>The reason it doesn’t work the ‘usual’ way is that when using Code First, your context extends DbContext instead of ObjectContext, and
>>Dynamic Data doesn’t know about DbContext.
Using separate namespaces/directories for ObjectContext does not solve the problem.
Did you manage to register the DbContext directly for DynamicData? Could you explain me how you call MetaModel.RegisterContext(). I would realy appreciate this!
I am using Entity Framework and not Linq2Sql. I think the bug is Entity Framework related, since it worked with Entity Framework before version 5 and 6.
It is good to hear, that it should work generally, so I have the hope to find a workaround. Could you please send me just the line where you call in your project
Hi Stefan, I asked Pranav on the asp.net team to have a look whilst I was at eh MVP Summit (Nov 2013) I'm not sure that there is an easy fix but we are looking into it.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
I updated Dynamic Data for Entity Framewiork 6 and it seems that they fixed this limitation. I have to verify it in more details (I dont use SqlServer), but it looks fine until now.
Thanks Steffan, I'm working through my bits to and will have an update soon. please post any result on where the ASP.Net team asked them to be posted :)
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Member
1 Points
14 Posts
Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Oct 28, 2013 09:08 AM|StefanG1|LINK
Hello!
I have the following issue. I use Dynamic Data with Entity Framework 5. I have multiple databases with each database having one ObjectContext. If I use for every ObjectContexts its own web project, everything runs fine. But, I am not able to register all ObjectContexts at once.
Doing it the direct way by registering the DbContext, I always get the exception "The context type 'MyContext' is not supported.".
For this, I am using the indirect way, to register the ObjectContext: metaModel.RegisterContext(() => { return ((IObjectContextAdapter)MyContext()).ObjectContext; }, ...);
This works as expected. But when I use this pattern more than once for various different Contexts, I get a "Duplicated Key" exception. It seems that the MetaModel uses the type name of ObjectContext as key in an internal dictionary.
On my search I found the DynamicData.EFCodeFirstProvider (see http://blog.davidebbo.com/2011/01/using-dynamic-data-with-ef-code-first.html). This should solve my problem. But when I use it, I get the same error again "The context type 'DynamicData.EFCodeFirstProvider.EFCodeFirstDataModelProvider' is not supported".
Stefan
Member
472 Points
167 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Oct 29, 2013 01:59 AM|BetsyASP|LINK
Hi StefanG,
Can you post your code to there.
Regards.
All-Star
17916 Points
5681 Posts
MVP
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Oct 29, 2013 09:46 AM|sjnaughton|LINK
Hi BetsyASP, see the post by David Ebbo and try it with EF5 or 6 using DbContext and you will get the error.
Always seeking an elegant solution.
Member
1 Points
14 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Oct 30, 2013 06:44 AM|StefanG1|LINK
This is what I do: I have two different DbContexts (any testdata will do), In global.asax I register both contexts:
MetaModel metaModel1 = new MetaModel();
metaModel1.RegisterContext(
() => { return ((IObjectContextAdapter)MyData1.GetDbContext()).ObjectContext; },
new ContextConfiguration() { ScaffoldAllTables = true });
MetaModel metaModel2 = new MetaModel();
metaModel2.RegisterContext(
() => { return ((IObjectContextAdapter)MyData2.GetDbContext()).ObjectContext; },
new ContextConfiguration() { ScaffoldAllTables = true });
Member
472 Points
167 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Oct 31, 2013 03:15 AM|BetsyASP|LINK
Try to read this article:
http://csharpbits.notaclue.net/2008/12/dynamic-data-registering-multiple.html
Member
1 Points
14 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Oct 31, 2013 06:09 AM|StefanG1|LINK
Hello BetsyASP! I was trying to do this. But when it did not work, I got in contact with sjnaughton and he confirmed me, that it does not work anymore with EF 5 and 6.
I add a code sample, you can easily reproduce the problem. I can start the project when one (the first or the second) MetaModel is commented. Both dont work.
The form Steve did it, does not work, I get an exception. See the commented code
//model.RegisterContext(typeof(DataModelContext),
// new ContextConfiguration() { ScaffoldAllTables = true });
Add following to global.asax:
Add two dummy contexts, as e.g. the followings:
In web.Config add two connection strings and create two dummy/empty database files:
Member
1 Points
14 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Nov 05, 2013 07:23 AM|StefanG1|LINK
Hello BetsyASP / sjnaughton do you have any news? Can you reproduce the issue, or do you need more information?
For me it is crucial to know, if the bug can be fixed. Else I whould have to redesign my complete concept.
Member
1 Points
14 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Nov 22, 2013 05:57 AM|StefanG1|LINK
Hello!
Any news on this topic? Will it be fixed? Any information whould be great. Our project highly depends on this.
Member
71 Points
48 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Nov 22, 2013 06:35 AM|valZ|LINK
If I understand the problem.
Try to place each DbContext in different directories. Accordingly, each DbContext will have its own name space. I act this way and it works great.
Member
1 Points
14 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Nov 22, 2013 07:01 AM|StefanG1|LINK
Helo valZ!
This does not work with Dynamic Data, as you always get the exception "The context type 'MyContext' is not supported.". See my initial post, or look at http://blog.davidebbo.com/2011/01/using-dynamic-data-with-ef-code-first.html where David Ebbo explains the problem:
>>Now we need to register our context with Dynamic Data, which is the part that requires special handling.
>>The reason it doesn’t work the ‘usual’ way is that when using Code First, your context extends DbContext instead of ObjectContext, and
>>Dynamic Data doesn’t know about DbContext.
Using separate namespaces/directories for ObjectContext does not solve the problem.
Did you manage to register the DbContext directly for DynamicData? Could you explain me how you call MetaModel.RegisterContext(). I would realy appreciate this!
Thanks,
Stefan
Member
71 Points
48 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Nov 22, 2013 08:27 AM|valZ|LINK
Oh, I'm sorry.
Of course I am using dynamic data, but with L2S. But everything else as I have described.
Member
1 Points
14 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Nov 23, 2013 10:16 AM|StefanG1|LINK
Thank you for the answer valZ.
I am using Entity Framework and not Linq2Sql. I think the bug is Entity Framework related, since it worked with Entity Framework before version 5 and 6.
It is good to hear, that it should work generally, so I have the hope to find a workaround. Could you please send me just the line where you call in your project
with the DbContext since I would like to test it.
Thanks,
Stefan
All-Star
17916 Points
5681 Posts
MVP
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Nov 28, 2013 05:09 AM|sjnaughton|LINK
Hi Stefan, I asked Pranav on the asp.net team to have a look whilst I was at eh MVP Summit (Nov 2013) I'm not sure that there is an easy fix but we are looking into it.
Always seeking an elegant solution.
Member
1 Points
14 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Nov 28, 2013 06:04 AM|StefanG1|LINK
Thanks Stephen!
Member
1 Points
14 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Dec 02, 2013 09:52 AM|StefanG1|LINK
Hello Stephen!
I found this stackoverflow article: http://stackoverflow.com/questions/19634997/dynamic-data-with-multiple-entity-framework-models/20265860?noredirect=1#comment30339875_20265860 . The case is quite similar, except that he does not use code first. Do you think, this could help us?
Stefan
All-Star
17916 Points
5681 Posts
MVP
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Dec 02, 2013 10:28 AM|sjnaughton|LINK
Hi Stefan, yes that will work as it still uses the ObjectContext I have several projects working that way but they will not work with the DBContext
Always seeking an elegant solution.
Member
1 Points
14 Posts
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Mar 04, 2014 09:52 AM|StefanG1|LINK
Hello!
I updated Dynamic Data for Entity Framewiork 6 and it seems that they fixed this limitation. I have to verify it in more details (I dont use SqlServer), but it looks fine until now.
Stefan
All-Star
17916 Points
5681 Posts
MVP
Re: Duplicated key when try to register multiple ObjectContexts in Dynamic Data
Mar 04, 2014 10:22 AM|sjnaughton|LINK
Thanks Steffan, I'm working through my bits to and will have an update soon. please post any result on where the ASP.Net team asked them to be posted :)
Always seeking an elegant solution.