EntityDataSource和Entity Framework 6

时间:2014-09-14 18:38:58

标签: c# asp.net entity-framework entity-framework-6

我正在学习ASP.NET。我来到EntityDataSorce控件。我正在使用EF6。我已经读过这个控件和EF6有一些问题,冲突,但随着对EntityDataSource的最后更新,这个问题已经解决了。 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

我正在尝试按照上述链接。首先,我创建一个.edmx模型

enter image description here

使用NuGet安装新的EntityDataSource Contro

enter image description here

我添加了两个EntityDataSource控件,并将其中一个的前缀更改为ef。所以我有两个控件,其中一个是旧的,另一个是新的更新

enter image description here

当我点击旧的时,我可以看到配置弹出窗口并进入配置数据源屏幕。但是当点击新的时,没有弹出窗口。那么,我该如何配置数据源呢? 这有什么问题?

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>

5 个答案:

答案 0 :(得分:15)

使用EF6时不支持用户界面。我们不再建议将实体数据源用于新项目,因此我们只是提供了可用于EF6的数据源。您需要直接在标记中执行配置。

答案 1 :(得分:2)

Dov Miller的答案很接近,但对我来说没有用,因为我不明白我为ContextTypeName命名的是什么。我使用设计器创建了我的模型,并且我没有在解决方案中找到从ObjectContext派生的任何名称。

所以我在看了很多帖子之后就这样做了:

  1. 添加了EntitySetName属性而不是ContextTypeName,指向我的类的Set属性。

  2. 选择EntityDataSource并双击事件OnContextCreating以创建事件方法。在其中输入以下代码以将DbContext转换为ObjectContext并解析IObjectContextAdapter以添加正确的using子句。

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

  3. 它最终作为gridview的数据源。

    this thread中找到上述解决方案,其中user2076170的答案显示了上面步骤2中的事件代码。我自己找到了第1步。

答案 2 :(得分:1)

我也遇到了这个问题,我找到了“模型绑定”http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data

这篇文章贯穿了构建“Code First”数据模型类的过程,但是我只是将ItemType与通过“EF Designer from database ...”生成的类一起切换,并且它工作正常。

希望这可以帮助仍在搜索的人。

麦克

答案 3 :(得分:1)

我在谢尔文的回答中从这个链接Entity DataSource not working with Entity Framework 6 Upgrade中了解到,您可以将旧的EntityDataSource从工具箱中删除到设计器,然后将标记前缀更改为ef而不是{{ 1}}。 (我在Seminda的链接中找到了对这个问题的评论)。之后,您可以在设计器和属性窗口中继续使用它,它可以工作。

那里的答案提到如果你使用EntityDataSource的事件,你必须改变背后的代码

asp

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

要将protected void OnContextCreating(object sender, Microsoft.AspNet.EntityDataSource.EntityDataSourceContextCreatingEventArgs e){... } 添加到Microsoft.AspNet.EntityDataSource

同时删除属性EventArgsDefaultContainerName,并在EntityDataSource上设置ConnectionString,如LMK在那里的评论所述。

我希望这可以帮助那些仍然希望将EntityDataSource与设计师一起使用的人,而不仅仅是通过标记。

答案 4 :(得分:0)

从2020年4月和EF6开始,您可以使用nuget安装Microsoft.AspNet.EntityDataSource,然后将DataSource的标签前缀从asp更改为ef。这是一些工作示例:

请参阅以上Dov Miller中有关属性差异的注释。

基础

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

带有WHERE子句

<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>