问题描述
我在EF4(VS2010 RC)中使用自我跟踪实体代码选项,我正在尝试使用动态数据来构建一个快速而肮脏的网站进行编辑。实体,数据上下文和EDMX文件都在单独的程序集中,当我在代码中调用所有内容时,该模型运行良好。但是当我尝试使用动态数据时,直接从蝙蝠中得到一大堆失败:
I'm using the Self-Tracking Entities codegen option in EF4 (VS2010 RC), and I'm trying to use Dynamic Data to build a quick-and-dirty website for editing. The Entities, Data Context, and EDMX files are all in separate assemblies, and this model works well when I call everything in code. But when I try to use it with Dynamic Data, right off the bat I get a whole lotta FAIL:
找不到Core.Recording的CLR类型。
在System.Data.Metadata.Edm.MetadataWorkspace.GetObjectSpaceType(StructuralType edmSpaceType)
在System.Web.DynamicData.ModelProviders.EFDataModelProvider.GetClrType(EntityType entityType)
在System.Web.DynamicData .ModelProviders.EFDataModelProvider.CreateTableProvider(EntitySet entitySet,EntityType entityType)
在System.Web.DynamicData.ModelProviders.EFDataModelProvider..ctor(Object contextInstance,Func 1 contextFactory)
1 contextFactory)
在系统.Web.DynamicData.ModelProviders.SchemaCreator.CreateDataModel(Object contextInstance,Func
在System.Web.DynamicData.MetaModel.RegisterContext(Func`1 contextFactory,ContextConfiguration配置)$ b D:\SimpleAdmin\Global.asax.cs中的SimpleAdmin.Global.RegisterRoutes(RouteCollection路由)中的$ b $ 32
在SimpleAdmin.Global.Application_Start(Object sender,EventArgs e)中D:\ SimpleAdmin\Global.asax.cs:第61行
Could not find the CLR type for 'Core.Recording'.at System.Data.Metadata.Edm.MetadataWorkspace.GetObjectSpaceType(StructuralType edmSpaceType) at System.Web.DynamicData.ModelProviders.EFDataModelProvider.GetClrType(EntityType entityType) at System.Web.DynamicData.ModelProviders.EFDataModelProvider.CreateTableProvider(EntitySet entitySet, EntityType entityType) at System.Web.DynamicData.ModelProviders.EFDataModelProvider..ctor(Object contextInstance, Func1 contextFactory) at System.Web.DynamicData.ModelProviders.SchemaCreator.CreateDataModel(Object contextInstance, Func
1 contextFactory) at System.Web.DynamicData.MetaModel.RegisterContext(Func`1 contextFactory, ContextConfiguration configuration) at SimpleAdmin.Global.RegisterRoutes(RouteCollection routes) in D:\SimpleAdmin\Global.asax.cs:line 32 at SimpleAdmin.Global.Application_Start(Object sender, EventArgs e) in D:\SimpleAdmin\Global.asax.cs:line 61
RegisterRoutes看起来ike this:
RegisterRoutes looks like this:
DefaultModel.RegisterContext((() => new DataContext.Entities()), new ContextConfiguration() { ScaffoldAllTables = true });
上下文中的默认构造函数已修改为使用我的连接字符串,如下所示:
The default constructor on the Context has been modified to use my my connection string, which looks like this:
<add name="Entities" connectionString="metadata=res://*/Entities.csdl|res://*/Entities.ssdl|res://*/Entities.msl;provider=System.Data.SqlClient;provider connection string="Data Source=xxxxxxxxxx;Initial Catalog=MyDB;Persist Security Info=True;User ID=xxxx;Password=xxxxxxx;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
我想我没有在连接字符串中正确引用O,C或CS空格。 ..但如果我在代码中调用上下文并使用它,它的工作正常。那么我做错了什么?
I imagine I'm not referencing the O, C, or CS spaces correctly in the connection string... yet it works just fine if I call the context up in code and use it. So what am I doing wrong?
谢谢!
推荐答案
,我找到了一个可行的解决方案。这源于这篇文章:
Actually, I found a workable solution. It stems from this post: http://thedatafarm.com/blog/data-access/wcf-data-services-and-ef-pocos-that-are-in-their-own-assembly/
由于我只使用动态数据网站来管理快速,而不是作为面向客户的生产站点,我不关心perf在场景中引入的问题。所以我添加了一个只有DynamicData将使用的构造函数:
Since I'm only using the Dynamic Data site for quick-and-dirty admin, and not as a customer-facing production site, I'm not concerned about the perf issues introduced in the scenario. So I added a constructor that only DynamicData would use:
public Entities(bool dynamicData)
: base(ConfigurationManager.ConnectionStrings["Entities"].ConnectionString, ContainerName)
{
Initialize();
var tracestring = this.CreateQuery<Address>("Entities.Addresses").ToTraceString();
}
然后,在Global.asax.cs的RegisterRoutes函数中,我现在有:
then, in Global.asax.cs' RegisterRoutes function, I now have this:
DefaultModel.RegisterContext((() => new DataContext.Entities(true)), new ContextConfiguration() { ScaffoldAllTables = true });
按照指示工作。金达刺激,但是每一个平台都必须拥有不一起玩的mod,对吧?
Works as directed. Kinda irritating,. but every platform has to have mods that don't play nice together, right?
HTH。
这篇关于实体框架4 +自我跟踪实体+ ASP.NET动态数据=错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!