0

How to create MetaTable from Entity Framework table or context? I am using code-first.

This is intended for use with some DynamicData methods

monkeyhouse
  • 2,875
  • 3
  • 27
  • 42

1 Answers1

0

Theres a few parts to this one:

On startup, make sure to register the dynamic data provider in global.asax

Global.asax.cs / other

App.DefaultModel.RegisterContext(
             new Microsoft.AspNet.DynamicData.ModelProviders.EFDataModelProvider(() => new MyDbContext()),
             new ContextConfiguration { ScaffoldAllTables = true });

App.cs ( a static class I use to store permanent references )

public static class App

  ...

   private static MetaModel s_defaultModel = new MetaModel();

   public static MetaModel DefaultModel
      {get{ return s_defaultModel; }}
}

where you need it

ModelMetaTable meta = App.DefaultModel.GetTable( nameof(db.MyAwsomeName) );

then you can do things like

MyAwesomeForm.SetMetaTable(table);

and the form will have all user controls like its in a normal dynamic data website

monkeyhouse
  • 2,875
  • 3
  • 27
  • 42