ASP.NET 动态数据看不到部分元数据“伙伴”班级
我有一个 ASP.NET 4 动态数据网站,它针对一组相当简单的数据库表运行,通过另一个程序集中的实体框架模型公开。我不想搭建 EF 模型中的所有表,因此在我的 global.asax 文件中,我已经初始化了默认模型,如下所示:
DefaultModel.RegisterContext( typeof( MyCompany.MyProject.DataModel.DataContext ), new ContextConfiguration() { ScaffoldAllTables = false } );
MSDN 文档(以及 global.asax 文件中的注释)说我现在应该能够通过将 [ScaffoldTable(true)]
属性添加到其部分“buddy”类来选择性地启用各个表的脚手架。我是这样做的:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
namespace MyCompany.MyProject.DataModel
{
[MetadataType( typeof( InHouseClaimMetadata ) )]
[ScaffoldTable( true )]
public partial class InHouseClaim
{
[DisplayName( "In-House Claims" )]
[TableName( "In-House Claims" )]
public class InHouseClaimMetadata
{
[DisplayName( "Reporting Date" )]
public object ReportingDate { get; set; }
// etc etc...
}
}
}
但是当加载 Default.aspx 时,我收到以下错误消息:
没有可访问的表。确保至少有一个数据模型 在 Global.asax 中注册并且脚手架已启用或实施 自定义页面。
我之前已经让它在类似的场景中工作过;此尝试的不同之处在于我的 EF 模型是它自己的程序集。如果我更改 global.asax 来继续构建所有表,它就可以正常工作。但显然,我不希望这样。我小心翼翼地确保部分元数据类的命名空间与 EF 数据上下文的命名空间相匹配。
所以我很困惑...
I have an ASP.NET 4 Dynamic Data web site that is running against a fairly simple set of database tables, exposed through an Entity Framework model in another assembly. I don't want to scaffold all of the tables in the EF model, so in my global.asax file, I've initialized the default model like this:
DefaultModel.RegisterContext( typeof( MyCompany.MyProject.DataModel.DataContext ), new ContextConfiguration() { ScaffoldAllTables = false } );
The MSDN docs (and the comments in the global.asax file) say that I should now be able to selectively enable scaffolding of individual tables by adding the [ScaffoldTable(true)]
attribute to their partial "buddy" class. I've done so like this:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
namespace MyCompany.MyProject.DataModel
{
[MetadataType( typeof( InHouseClaimMetadata ) )]
[ScaffoldTable( true )]
public partial class InHouseClaim
{
[DisplayName( "In-House Claims" )]
[TableName( "In-House Claims" )]
public class InHouseClaimMetadata
{
[DisplayName( "Reporting Date" )]
public object ReportingDate { get; set; }
// etc etc...
}
}
}
But when loading Default.aspx, I get the following error message:
There are no accessible tables. Make sure that at least one data model
is registered in Global.asax and scaffolding is enabled or implement
custom pages.
I've gotten this to work in similar scenarios before; the one thing that is different about this attempt is that my EF model is its own assembly. If I change the global.asax to go ahead and scaffold all tables, it works fine. But obviously, I don't want that. I was careful to make sure that the namespace for the partial metadata class matches the namespace of the EF data context.
So I'm stumped...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以,我是个白痴:这不是 EF 或动态数据问题,而是 C# 约束。来自 MSDN:
So, I'm an idiot: this isn't an EF or Dynamic Data problem, it is a C# constraint. From MSDN:
我尝试重新创建您的场景,而不是使用属性映射,而是使用以下代码进行了测试:
如果 EF 数据上下文的命名空间与部分类的命名空间匹配,则此方法有效。您能否尝试注释掉您的属性映射以消除这些问题,然后看看您如何处理?
I've tried to re-create your scenario, and instead of using the property mappging I've tested using the following code:
This works if the namespace of the EF data context matches that of the partial classes. Can you try and comment out your property mappings to eliminate those as an issue, and see how you go from there?
对我有用的是,在解决方案资源管理器中,右键单击包含我的部分类的 .cs 文件,选择“属性”,并将“构建操作”设置为“编译”。无论出于何种原因,文件的“构建操作”默认设置为“内容”。 (我花了几个小时才弄清楚这一点。希望这能节省别人的时间。)
What worked for me was, in Solution Explorer, right clicking on the .cs file that contains my partial classes, choosing Properties, and setting Build Action to Compile. For whatever reason, the file's Build Action was set to Content by default. (Took me hours to figure this out. Hopefully, this will save someone the time.)