Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 472
Matt Peterson Avatar asked Jan 06 '12 22:01

Matt Peterson


2 Answers

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.

like image 133
Matt Peterson Avatar answered Sep 24 '22 06:09

Matt Peterson


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?

like image 36
LewisBenge Avatar answered Sep 22 '22 06:09

LewisBenge