共用方式為


Preview of Dynamic Data Provider and EntityDataSource Control for EF6

This post was originally posted on the .NET Web Development and Tools Blog .

We are pleased to announce an update to ASP.NET DynamicData and EntityDataSource control, so that they work with Entity Framework 6.

 

What’s in this preview

  • DynamicData provider for Entity Framework 6
  • EntityDataSource control for Entity Framework 6

 

How to install this preview

You can download this preview for ASP.NET DynamicData.EFProvider (https://www.nuget.org/packages/Microsoft.AspNet.DynamicData.EFProvider/) and EntityDataSource (https://www.nuget.org/packages/Microsoft.AspNet.EntityDataSource/) as preview NuGet packages from the NuGet gallery. You can install these pre-release packages through NuGet using the NuGet Package Manager Console, like this:

  • Install-Package Microsoft.AspNet.DynamicData.EFProvider -Version 6.0.0-alpha1 –Pre
  • Install-Package Microsoft.AspNet.EntityDataSource -Version 6.0.0-alpha1 –Pre

 

Getting started

Microsoft.AspNet.DynamicData.EFProvider

This package has a DynamicData EFProvider for Entity Framework 6. This provider can work with a Model (either Code First or EF Designer) which was created using Entity Framework 6. This package also installs the Page Templates, Entity Templates and Field Templates which are required for DynamicData. The templates have been updated to use Microsoft.AspNet.EntityDataSource control which we are also previewing today as well.

For more information on ASP.NET DynamicData please see https://msdn.microsoft.com/en-us/library/cc488545.aspx

Following are the steps for using this package in a ASP.NET DynamicData application:

  • Create a new ASP.NET Dynamic Data Entities Web Application

  • Add the Microsoft.AspNet.DynamicData.EFProvider NuGet package

  • This will do the following

    • Add a reference to the DynamicData EFProvider binary
    • Install the templates. If you are starting with a new project, then you can override the templates. If you have an existing application, then you should be careful when overriding the changes. The templates replace the EntityDataSource control which shipped in .NET with Microsoft.AspNet.EntityDataSource and also have a few bug fixes in the Many-Many field template so they work with Entity Framework 6.
  • Create your model using Entity Framework Code First or EF Designer.

  • Add the following code in RegisterRoutes in Global.asax.cs to register your EF Model:

     DefaultModel.RegisterContext(
        new Microsoft.AspNet.DynamicData.ModelProviders.EFDataModelProvider(
            () => newNorthwindEntities1()),
        newContextConfiguration { ScaffoldAllTables = true });
    
  • Run the project

  • You would see all the tables listed on the Default page.

Microsoft.AspNet.EntityDataSource Control

This is an update to the EntityDataSource control which shipped in the .NET Framework. The EntityDataSource control has been updated to work with Entity Framework 6.

To use this control, please do the following

  • Create an ASP.NET application

  • Install the packageMicrosoft.AspNet.EntityDataSource

    • This package will
      • install the runtime binary for Microsoft.AspNet.EntityDataSource

      • Install the EntityFramework version 6 NuGet package

      • Add the following tag prefix in web.config

         <pages> 
          <controls> 
            <add 
              tagPrefix="ef" 
              assembly="Microsoft.AspNet.EntityDataSource" 
              namespace="Microsoft.AspNet.EntityDataSource" /> 
          </controls> 
        </pages> 
        
  • Create a new Web Form page

  • Use the control as follows and bind it to any Databound control such as GridView, FormView etc.

     <ef:EntityDataSource ID="GridDataSource" runat="server" EnableDelete="true" /> 
    
 

Known Issues

  • The templates in Microsoft.AspNet.DynamicData.EFProvider are for C# only.
  • The templates in Microsoft.AspNet.DynamicData.EFProvider are for Web Application projects only and will not work for WebSites projects.

 

Give feedback

If you find any issues with this preview, please file issues at the EntityFramework CodePlex site https://entityframework.codeplex.com

Thank you for trying out the preview and helping us make it better.

Comments

  • Anonymous
    February 13, 2014
    I tried the new EntityDataSource control. However, I ran into one problem. I'm using Telerik's RadGrid control for ASP.NET Web Forms. That control has built-in filtering. When using it with EF 5, the control does the filtering in the database. This is the main reason why I'm using the control. Otherwise, I wouldn't bother with it. I found that with EF 6 and the new EntityDataSource control, the filtering does not happen in the SQL Server. It happens in the web server. I'm not sure if this is an issue with Telerik's control or if there is something that has changed with the EntityDataSource control that is causing this to happen. In any case, it's pretty much a blocker, because I'm using the grid wit data sets that are large and it will result in an OutOfMemoryError without the filtering happening in the database. Note, paging works correctly, just not filtering.

  • Anonymous
    February 14, 2014
    @Jon - We got your email with additional details and we're digging into the issue now. Thanks for raising it with us!

  • Anonymous
    April 01, 2014
    I converted EF5 DD project with your guide, but it doesn't work blogs.msdn.com/.../announcing-preview-of-dynamic-data-provider-and-entitydatasource-control-for-entity-framework-6.aspx Then i Put DefaultModel.RegisterContext(             new Microsoft.AspNet.DynamicData.ModelProviders.EFDataModelProvider(() =>                 RendicontazioneEntities()),                 newContextConfiguration { ScaffoldAllTables = true }); I received this errors Error 8 'RendicontazioneEntities' is a 'type' but is used like a 'variable' Before i Used  DefaultModel.RegisterContext(() =>         {             return ((System.Data.Entity.Infrastructure.IObjectContextAdapter)new RendicontazioneEntities()).ObjectContext;         }, new ContextConfiguration() { ScaffoldAllTables = true }); Can you help me please? thanks

  • Anonymous
    April 07, 2014
    @Error please help me - Looks like you are missing the 'new' keyword before 'RendicontazioneEntities()'.

  • Anonymous
    April 20, 2014
               DefaultModel.RegisterContext(                new Microsoft.AspNet.DynamicData.ModelProviders.EFDataModelProvider(                    () =>new InvestorHomesEntities()), new ContextConfiguration { ScaffoldAllTables = true });

  • Anonymous
    May 06, 2014
    I am still getting the 'Cannot implicitly convert type 'System.Data.Objects.ObjectContext' to 'System.Data.Entity.Core.Objects.ObjectContext'" error. I have installed the new packages, updated the global.asax and updated the web.config for the tagPrefix ef. Help!

  • Anonymous
    May 07, 2014
    @Sabrina - It sounds like you are trying to pass an EF5 ObjectContext to the EF6 version of the Entity Data Source (or vice versa). You can start up a StackOverflow question and be sure to include code that reproduces the error. Feel free to reply here with a link to the question and we'll make sure it gets answered.

  • Anonymous
    September 14, 2014
    I have encounter a problem. Please take a at this page stackoverflow.com/.../entitydatasource-and-entity-framework-6

  • Anonymous
    September 15, 2014
    @Omer - I responded to your StackOverflow post.

  • Anonymous
    February 26, 2015
    The comment has been removed

  • Anonymous
    March 13, 2015
    @Chris - If you have a specific issue that you are hitting then StackOverflow.com (with the entity-framework tag) is the best place to ask. Be sure to include example code and details of the exception/error you are hitting. Out of date documentation is definitely a challenge. We have been trying to make this better by consolidating docs at msdn.com/data/ef and keeping a single version of each doc up to date... but there is still a bunch of legacy documentation out there that doesn't follow this model. We've tried to tidy up a lot of the context and blog posts we own... but a lot of it isn't owned/published by our team.

  • Anonymous
    June 17, 2015
    The comment has been removed

  • Anonymous
    June 17, 2015
    In addition to above comment: I'm getting "The context type 'MyEntities' is not supported" error if I try the below one: DefaultModel.RegisterContext(              () =>              {                  var context = ((IObjectContextAdapter)new MyEntities()).ObjectContext;                  return context;              },              new ContextConfiguration() { ScaffoldAllTables = true });

  • Anonymous
    September 23, 2015
    Unable to find package 'Microsoft.AspNet.DynamicData.EFProvider'.

  • Anonymous
    November 18, 2015
    @Boris - here is a link to the package on NuGet www.nuget.org/.../Microsoft.AspNet.DynamicData.EFProvider

  • Anonymous
    November 19, 2015
    @Dave - There is a constructor on DbContext that takes an ObjectContext, this should allow you to easily create a DbContext that wraps your ObjectContext and then pass the DbContext to the data source. You don't need to create a derived DbContext for this, you can just create an instance of DbContext directly.