6

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

Matt Peterson
  • 5,169
  • 4
  • 32
  • 34
  • This guy was way ahead of me: http://stackoverflow.com/questions/647385/is-it-possible-to-have-two-partial-classes-in-different-assemblies-represent-the – Matt Peterson Jan 09 '12 at 16:09

3 Answers3

4

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.

Matt Peterson
  • 5,169
  • 4
  • 32
  • 34
1

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

D_T
  • 191
  • 1
  • 4
1

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?

LewisBenge
  • 2,706
  • 16
  • 20
  • No luck. But your answer led me to stripping my code down to the bare minimum scenario to identify the problem more clearly. – Matt Peterson Jan 09 '12 at 15:59