ASP.NET Dynamic Data not seeing partial metadata "buddy" class

3.1k Views Asked by At

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...

3

There are 3 best solutions below

0
On BEST ANSWER

So, I'm an idiot: this isn't an EF or Dynamic Data problem, it is a C# constraint. From MSDN:

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

1
On

I've tried to re-create your scenario, and instead of using the property mappging I've 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 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?

1
On

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.)