简体   繁体   中英

How to create MetaTable from Entity Framework table

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

This is intended for use with some DynamicData methods

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM