Question

I am using Dynamic Data with Entity framework models. If I use this with 1 EF model, then this works like a charm.

But now I need to use multiple EF models in my Dynamic Data Project and I'm receiving errors during the registration process.

Code:

    public static void RegisterRoutes(RouteCollection routes)
    {
        var model1 = new MetaModel();
        model1.RegisterContext(() =>
        {
            return ((IObjectContextAdapter)new Model1Entities()).ObjectContext;
        }, new ContextConfiguration() { ScaffoldAllTables = true });

        routes.Add(new DynamicDataRoute("model1/{table}/{action}.aspx")
        {
            Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
            Model = model1
        });


        var model2 = new MetaModel();
        model2.RegisterContext(() =>
        {
            return ((IObjectContextAdapter)new Model2Entities()).ObjectContext;
        }, new ContextConfiguration() { ScaffoldAllTables = true });

        routes.Add(new DynamicDataRoute("model2/{table}/{action}.aspx")
        {
            Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
            Model = model2
        });
    }

On runtime I'm receiving an error when he's executing model2.RegisterContext .

Error:

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

So for model1 he can register the context but for model2 he's blocked on this error.

If you know how to solve this, please advise!

Was it helpful?

Solution

I got it to work in 2 steps:

  • by removing the '.tt' files in my entity model.
  • in your edmx model, putting the property 'Code Generation Strategy' to 'Default' of your model. (and rebuild your solution)

After this he accepted the registration of multiple entities.

OTHER TIPS

Hy Tom,

I have the same issue since more than one month. I was investigating many time into this, since it is blocking my project. I started a forum entry here:

http://forums.asp.net/t/1946475.aspx?Duplicated+key+when+try+to+register+multiple+ObjectContexts+in+Dynamic+Data

It worked in previous versions. There was a workaround (see http://blog.davidebbo.com/2011/01/using-dynamic-data-with-ef-code-first.html) but this does not help anymore for current versions.

In short: it is an ASP.Net bug. Since we cannot call DbContext directly (which would be distinguishable) and we have to call ObjectContext, and the internal dictionary for the MetaModel uses the type without namespace as key (i.e. always "ObjectType"), there is no way around it.

Luckily, the ASP.Net team is now investigating into it now. Keep informed on the mentioned forum page.

UPDATE MARCH 2014: The current version http://blogs.msdn.com/b/webdev/archive/2014/02/28/announcing-the-release-of-dynamic-data-provider-and-entitydatasource-control-for-entity-framework-6.aspx fixes the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top