How to bind EF Code First DbContext to an Asp.Net DataSource?

前端 未结 1 1273
一个人的身影
一个人的身影 2020-12-30 13:55

I\'ve created the following Context to be used with Entity Framework Code First:

public class Context : DbContext
             


        
相关标签:
1条回答
  • 2020-12-30 14:28

    You can use EntityDataSource as source for your GridView and implement handler for ContextCreating event:

    protected void DataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
    {
        var context = new Context();
        e.Context = ((IObjectContextAdapter)context).ObjectContext;
    }
    

    Then you just need to configure the data source in the page. EntitySetName should be hopefully same as your DbSet property name exposed on the context.

    Other way is using ObjectDataSource which will make a bridge between GridView and DbSet<Animal> but this can be more complex especially if you want bi-didrectional data binding.

    0 讨论(0)
提交回复
热议问题