locked
Duplicated key when try to register multiple ObjectContexts in Dynamic Data RRS feed

  • Question

  • User-1657122598 posted

    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

    Monday, October 28, 2013 9:08 AM

All replies

  • User521424079 posted

    Hi StefanG,

    Can you post your code to there.

    Regards.

    Tuesday, October 29, 2013 1:59 AM
  • User-330204900 posted

    Hi BetsyASP, see the post by David Ebbo and try it with EF5 or 6 using DbContext and you will get the error.

    Tuesday, October 29, 2013 9:46 AM
  • User-1657122598 posted

    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 });

    Wednesday, October 30, 2013 6:44 AM
  • Thursday, October 31, 2013 3:15 AM
  • User-1657122598 posted
     

    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:

     

      <connectionStrings>
        <add
            name="DataContextConnectionString"
            connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Tests\TestData1.mdf;Integrated Security=True;Connect Timeout=30"
            providerName="System.Data.SqlClient" />
        <add
            name="DataContext2ConnectionString"
            connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Tests\TestData2.mdf;Integrated Security=True;Connect Timeout=30"
            providerName="System.Data.SqlClient" />
      </connectionStrings>

    Thursday, October 31, 2013 6:09 AM
  • User-1657122598 posted

    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.

    Tuesday, November 5, 2013 7:23 AM
  • User-1657122598 posted

    Hello!

    Any news on this topic? Will it be fixed? Any information whould be great. Our project highly depends on this.

    Friday, November 22, 2013 5:57 AM
  • User350138131 posted

    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.

    Friday, November 22, 2013 6:35 AM
  • User-1657122598 posted

    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

    Friday, November 22, 2013 7:01 AM
  • User350138131 posted

    Oh, I'm sorry.

    Of course I am using dynamic data, but with L2S. But everything else as I have described.

    Friday, November 22, 2013 8:27 AM
  • User-1657122598 posted

    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

    model1.RegisterContext

    with the DbContext since I would like to test it.

    Thanks,

      Stefan

    Saturday, November 23, 2013 10:16 AM
  • User-330204900 posted

    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.

    Thursday, November 28, 2013 5:09 AM
  • User-1657122598 posted

    Thanks Stephen!

    Thursday, November 28, 2013 6:04 AM
  • User-1657122598 posted

    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

    Monday, December 2, 2013 9:52 AM
  • User-330204900 posted

    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

    Monday, December 2, 2013 10:28 AM
  • User-1657122598 posted

    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

    Tuesday, March 4, 2014 9:52 AM
  • User-330204900 posted

    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 :)

    Tuesday, March 4, 2014 10:22 AM