9

I am learning ASP.NET. I came to EntityDataSorce control. I am using EF6. I have read that this control and EF6 have some issues, conflicts, but with the last update to EntityDataSource this issue has solved. http://blogs.msdn.com/b/webdev/archive/2014/02/28/announcing-the-release-of-dynamic-data-provider-and-entitydatasource-control-for-entity-framework-6.aspx

I am trying to follow above link. First I create an .edmx model

enter image description here

Install new EntityDataSource Contro with NuGet

enter image description here

I added two EntityDataSource controls and changed prefix of one of them to ef. So I have two control one of them is old and other one is new updated

enter image description here

When I click the old one I can see the configuration popup and reach the Configure Data Source screen. But when click on the new one there is no popup. So, how can I configure data source? What is wrong with this?

enter image description here

Web.config

    <?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Web.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5"/>
    <pages>
      <controls>
        <add tagPrefix="ef" assembly="Microsoft.AspNet.EntityDataSource" namespace="Microsoft.AspNet.EntityDataSource"/>
      </controls>
    </pages>
  </system.web>
  <connectionStrings>
    <add name="SampleDbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=OMER-HP\SQLEXPRESS2014OK;initial catalog=SampleDb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/>
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
</configuration>

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <ef:EntityDataSource ID="EntityDataSourceNew" runat="server">
        </ef:EntityDataSource>
        <br />
        <asp:EntityDataSource ID="EntityDataSourceOld" runat="server">
        </asp:EntityDataSource>

    </div>
    </form>
</body>
</html>
Omer
  • 8,194
  • 13
  • 74
  • 92

5 Answers5

15

The user interface isn't supported when using EF6. We no longer recommend using the Entity Data Source for new projects, so we just did the work to provide a data source that is usable with EF6. You need to perform configuration directly in the markup.

Rowan Miller
  • 2,090
  • 15
  • 15
  • 1
    Hi Rowan, I have seen your very helpful videos at [MSDN entity framework blog.](http://channel9.msdn.com/Blogs/EF/) The [Code First to Existing Database was just great.](http://channel9.msdn.com/Blogs/EF/Code-First-to-Existing-Database-EF6-1-Onwards-). We are currently struggling to consume a third party web service into EE model. Can you share a video how to go about it. The third party is ERP software. We have an asp.net web module we need to set up to consume this web service. – bonCodigo Sep 24 '14 at 03:58
  • 15
    What' the recommended way for new projects ? – user957479 Mar 29 '15 at 09:02
  • 3
    I second @user957479. Can you elaborate on the recommended approach for new projects? Are you perhaps suggesting that model binding should be used instead? But what if someone has a `QueryableExtender` attached to the datasource? Can that be replicated with model binding as cleanly? – julealgon Jun 02 '15 at 15:05
  • 2
    Pure and Simple frustration ... I ask you and I ask for a reason ?!!?! – Vincenzo Costa Nov 18 '15 at 09:23
  • 2
    Good question, @user957479. I thought I'd use this new project I'm working on as an opportunity to play with EF in a WebForms project, but I guess I'll give up and go back to creating Stored Procedures in my database. It may be clunky but at least it works and I know what I'm doing. One day I'll learn MVC... – Philip Stratford Aug 19 '16 at 13:30
2

Dov Miller's answer was close but didn't work for me because I didn't understand what name I put for ContextTypeName. I created my model with the designer and I didn't find any name derived from ObjectContext in the solution.

So I did this after looking at many threads:

  1. Instead of ContextTypeName, added EntitySetName property pointing to the Set property of my class.

  2. Selected EntityDataSource and double-clicked on the event OnContextCreating to create an event method. Entered the following code in it to convert the DbContext into ObjectContext and resolved the IObjectContextAdapter to add proper using clause.

    var context = new MyModelContainer(); e.Context = ((IObjectContextAdapter)context).ObjectContext;

It finally worked as a datasource for the gridview.

Found the above solution in this thread where the answer by user2076170 shows the event code in step 2 above. I found step 1 on my own.

user173399
  • 464
  • 5
  • 21
  • The ContextTypeName is the Entity Container Name in the edmx Diagram properties. If the edmx file is in a different Project in the solution, add the Project name like this MyProject.MyEntities, and if the Project has a special sufixe, like DAL, add it too, MyProject.DAL.MyEntities. – Dov Miller Jul 30 '17 at 10:31
  • If I'm not mistakened this answer is good for EF4 but not for EF6 which the question relates to. – Dov Miller Jul 30 '17 at 11:43
1

I ran into this problem as well and I found "Model Binding" http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data

The post runs through the process of building "Code First" data model classes, however I just switched the ItemType with the classes generated through the "EF Designer from database..." process and it worked just fine.

Hope this helps someone who is still searching.

Mike

Mike
  • 788
  • 10
  • 27
1

I learned from this link Entity DataSource not working with Entity Framework 6 Upgrade in Sergey's answer, that you can drop the old EntityDataSource from the toolbox to the designer and then change the tag prefix to ef instead of asp. (I got to the link in Seminda's comment to the question here). After that you can continue working with it in the designer and properties window and it works.

The answer there mentions that if you use events of the EntityDataSource you have to change the code behind

From

protected void OnContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e){... } 

TO

protected void OnContextCreating(object sender, Microsoft.AspNet.EntityDataSource.EntityDataSourceContextCreatingEventArgs e){... } 

The point being to add Microsoft.AspNet.EntityDataSource to the EventArgs.

Also remove the properties DefaultContainerName and ConnectionString, and just set ContextTypeName on the EntityDataSource, as mentioned in LMK's comment there.

I hope this can help those who still want to use the EntityDataSource with the designer and not only thru markup.

Dov Miller
  • 1,958
  • 5
  • 34
  • 46
0

As of April 2020 and EF6, you can use nuget to install Microsoft.AspNet.EntityDataSource, then change the tag prefix of your DataSource from asp to ef. Here are some working examples:

See the notes above from Dov Miller regarding the attribute differences.

The Basics

<ef:EntityDataSource ID="anyIdYouWant" runat="server" ContextTypeName="yourFullyQualifedEFClassName" EntitySetName="nameOfEntitiesOrTable" />

With a WHERE clause

<ef:EntityDataSource ID="anyIdYouWant" runat="server" ContextTypeName="yourFullyQualifedEFClassName" EntitySetName="nameOfEntitiesOrTable"
    EntityTypeFilter="nameOfEntitiesOrTable" Where="it.someProperty = @someProperty">
    <WhereParameters>
        <asp:SessionParameter DbType="Int32" Direction="Input" Name="someProperty" SessionField="someFieldOnYourForm" />
    </WhereParameters>
</ef:EntityDataSource>
rainabba
  • 3,804
  • 35
  • 35