gpt4 book ai didi

c# - ASP+EF加载程序集问题

转载 作者:行者123 更新时间:2023-11-30 22:42:49 24 4
gpt4 key购买 nike

我有一个大问题。当我运行 asp 时,应用程序有时会崩溃并显示以下错误消息:

Schema specified is not valid. Errors: The types in the assembly 'Data.EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be loaded because the assembly contains the EdmSchemaAttribute, and the closure of types is being loaded by name. Loading by both name and attribute is not allowed.

堆栈跟踪:

[MetadataException: Schema specified is not valid. Errors: The types in the assembly 'Data.EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be loaded because the assembly contains the EdmSchemaAttribute, and the closure of types is being loaded by name. Loading by both name and attribute is not allowed.]
System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action1 logLoadMessage) +480<br/>
System.Data.Metadata.Edm.ObjectItemCollection.ExplicitLoadFromAssembly(Assembly
assembly, EdmItemCollection
edmItemCollection, Action
1 logLoadMessage) +53
System.Data.Metadata.Edm.MetadataWorkspace.ExplicitLoadFromAssembly(Assembly assembly, ObjectItemCollection collection, Action1 logLoadMessage)
+93 System.Data.Metadata.Edm.MetadataWorkspace.LoadFromAssembly(Assembly
assembly, Action
1 logLoadMessage) +130 System.Web.UI.WebControls.EntityDataSourceView.ConstructContext() +585 System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +76
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143 Telerik.Web.UI.GridTableView.PerformSelect() +38 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74 Telerik.Web.UI.GridTableView.DataBind() +363 Telerik.Web.UI.RadGrid.DataBind() +173 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75 System.Web.UI.Control.EnsureChildControls() +102 Telerik.Web.UI.GridBaseDataList.get_Controls() +33 Telerik.Web.UI.RadAjaxControl.PopulatePlainPanels(Control parent, List`1 list, Control root) +119 Telerik.Web.UI.RadAjaxControl.OnPagePreRender(Object sender, EventArgs e) +1802
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.Control.OnPreRender(EventArgs e) +8864486
System.Web.UI.Control.PreRenderRecursiveInternal() +103 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496

我还在每个上下文调用之前添加了 LoadFromAssembly,但仍然是同样的问题。

context.MetadataWorkspace.LoadFromAssembly(context.GetType().Assembly);

环境:VS 2010、.NET 4.0、C#、EF

有人可以帮我解决这个问题吗?

提前致谢

最好的问候。

最佳答案

我在混合使用 EntityDataSource 对象和其他数据访问(例如 ObjectDataSource 和/或命令式代码)的页面上收到了同样的错误。

我采纳了您问题的其他答案中引用的 MSDN 文章的建议。 IE。使用 context.MetadataWorkspace.LoadFromAssembly(...)。我实际上不知道如何获取对 EntityDataSource 使用的上下文实例的引用,而且我想无论如何我都应该把它弄干,所以我创建了一个部分类,例如:

    public partial class YourTypeNameEntities
{
partial void OnContextCreated()
{
this.MetadataWorkspace.LoadFromAssembly(typeof(Full.Namespace.Of.YourTypeNameEntities).Assembly);

}

它仍然不起作用,但我很快注意到,当在绑定(bind)到 EntityDataSource 的控件上调用 DataBind() 时,我的 OnContextCreated() 实现没有触发!

这是一个不同的问题,我找到了解决方案...用 ContextTypeName 替换 EntityDataSource 声明中的 ConnectionString 和 DefaultContainerName 属性。例如:

    <asp:EntityDataSource ID="CountrySource" runat="server" 
ContextTypeName="Full.Namespace.Of.YourTypeNameEntities" EntitySetName="Country"
OrderBy="it.Name" Where="it.Active==true">

这确保了我的 OnContextCreated 实现会触发并且 POOF,“不允许同时通过名称和属性加载”问题消失了!

关于c# - ASP+EF加载程序集问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4212702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com