ASP.NET Dynamic Data Does Not See Partial Buddy Metadata

I have an ASP.NET 4 Dynamic Data website that works with a fairly simple set of database tables that display through the Entity Framework model in a different assembly. I do not want to throw all the tables in the EF model, so in my global.asax file, I initialized the default model as follows:

DefaultModel.RegisterContext( typeof( MyCompany.MyProject.DataModel.DataContext ), new ContextConfiguration() { ScaffoldAllTables = false } );

MSDN docs (and comments in the global.asax file) say that now I should be able to selectively include the scaffolds of individual tables by adding the [ScaffoldTable(true)] attribute to their partial buddy class. I did 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, the following error message appears:

No tables available. Make sure that at least one data model is registered in Global.asax, and that scaffolding is enabled or custom pages are implemented.

I got this to work in similar scenarios before; one thing that differs from this attempt is that my EF model is its own assembly. If I modify global.asax to move forward and align all tables, it works fine. But obviously I do not want this. I was careful to make sure that the namespace for the partial metadata class matches the EF data context namespace.

So I'm at a dead end ...

+6
source share
3 answers

So I'm an idiot: this is not an EF or Dynamic Data problem, this is a C # limitation. From MSDN :

All partial type definitions intended for parts of the same type must be defined in the same assembly and in the same module (.exe or .dll file). Partial definitions cannot span multiple modules.

+4
source

I tried to recreate your script and instead of using the mappging property, which I tested using the following code:

 using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Web.DynamicData; namespace MyCompany.MyProject.DataModel { [MetadataType(typeof(InHouseClaimMetadata))] [ScaffoldTable(true)] public partial class InHouseClaim { public class InHouseClaimMetadata { } } } 

This works if the EF data context namespace matches the partial class namespace. Can you try to comment on your property mappings to fix them as a problem and see how you get from there?

+1
source

What worked for me was in Solution Explorer, right-clicking on the .cs file that contains my partial classes, selecting "Properties" and installing Build Action to Compile. For some reason, the Build Action file was set to Content by default. (Took a few hours to figure it out. I hope this saves someone time.)

+1
source

Source: https://habr.com/ru/post/905363/


All Articles