C# EntityDataSource和实体框架6
我正在学习ASP.NET。我来到EntityDataSorce控制中心。我正在使用EF6。我已经读到这个控件和EF6有一些问题和冲突,但是随着EntityDataSource的最后一次更新,这个问题已经解决了 我正试图遵循上述链接。首先,我创建一个.edmx模型 使用NuGet安装新EntityDataSource Contro 我添加了两个EntityDataSource控件,并将其中一个控件的前缀更改为ef。所以我有两个控件,一个是旧控件,另一个是新控件 当我点击旧的,我可以看到配置弹出窗口,并达到配置数据源屏幕。但是当点击新的按钮时,没有弹出窗口。那么,如何配置数据源呢? 这有什么问题 Web.configC# EntityDataSource和实体框架6,c#,asp.net,entity-framework,entity-framework-6,C#,Asp.net,Entity Framework,Entity Framework 6,我正在学习ASP.NET。我来到EntityDataSorce控制中心。我正在使用EF6。我已经读到这个控件和EF6有一些问题和冲突,但是随着EntityDataSource的最后一次更新,这个问题已经解决了 我正试图遵循上述链接。首先,我创建一个.edmx模型 使用NuGet安装新EntityDataSource Contro 我添加了两个EntityDataSource控件,并将其中一个控件的前缀更改为ef。所以我有两个控件,一个是旧控件,另一个是新控件 当我点击旧的,我可以看到配置弹
<?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="data source=OMER-HP\SQLEXPRESS2014OK;initial catalog=SampleDb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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>
使用EF6时不支持用户界面。我们不再建议在新项目中使用实体数据源,所以我们只是提供了一个可用于EF6的数据源。您需要直接在标记中执行配置。我也遇到了这个问题,发现了“模型绑定”
这篇文章贯穿了构建“代码优先”数据模型类的过程,但我只是将ItemType与通过“EF Designer from database…”过程生成的类进行了切换,效果很好
希望这对仍在搜索的人有所帮助
Mike我从Sergey回答中的这个链接中了解到,您可以将旧EntityDataSource从工具箱中删除到设计器中,然后将标记前缀更改为ef
,而不是asp
。(我在Seminda对这个问题的评论中找到了链接)。之后,您可以在designer和properties窗口中继续使用它,它就可以工作了
这里的答案提到,如果使用EntityDataSource的事件,则必须更改隐藏的代码
从
到
要点是将Microsoft.AspNet.EntityDataSource
添加到EventArgs
同时删除属性DefaultContainerName
和ConnectionString
,只需在EntityDataSource上设置ContextTypeName
,如LMK的评论所述
我希望这可以帮助那些仍然希望在设计器中使用EntityDataSource的人,而不仅仅是通过标记。Dov Miller的答案很接近,但对我来说不起作用,因为我不明白我为ContextTypeName取了什么名称。我用设计器创建了模型,但在解决方案中找不到任何派生自ObjectContext的名称 因此,我在查看了许多线程后进行了此操作:
找到了上面的解决方案,其中user2076170的答案显示了上面步骤2中的事件代码。我自己找到了步骤1。从2020年4月开始,EF6可以使用nuget进行安装,然后将数据源的标记前缀从asp更改为ef。以下是一些工作示例: 有关属性差异,请参见上面的注释 基础知识
<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>
删除EntityDataSource并再次添加。这可能也有用,这可能会对你有所帮助@Seminda很遗憾,他们没有帮助Hi Rowan,我在网站上看过你非常有用的视频。我们目前正在努力将第三方web服务消费到EE模型中。你能分享一段视频吗。第三方是ERP软件。我们需要设置一个asp.net web模块来使用此web服务。对于新项目,建议使用什么方法?I second@user957479。你能详细介绍一下新项目的推荐方法吗?您是否建议改为使用模型绑定?但是,如果有人将QueryableExtender
附加到数据源,该怎么办?模型绑定可以像复制一样干净吗?纯粹而简单的沮丧。。。我问你,我问你一个理由?!!?!问得好,@user957479。我想我会利用我正在从事的这个新项目作为在WebForms项目中使用EF的机会,但我想我会放弃,转而在数据库中创建存储过程。它可能很笨重,但至少它能工作,我知道我在做什么。总有一天我会学习MVC…ContextTypeName是edmx图表属性中的实体容器名称。如果edmx文件位于解决方案中的另一个项目中,请添加项目名称,如MyProject.MyEntities,如果该项目具有特殊的sufixe,如DAL,请也添加它,如MyProject.DAL.MyEntities。如果我没有误解,此答案对EF4是好的,但对与问题相关的EF6不是。
<ef:EntityDataSource ID="anyIdYouWant" runat="server" ContextTypeName="yourFullyQualifedEFClassName" EntitySetName="nameOfEntitiesOrTable" />
<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>