Dynamic Data with Entity Framework... using [ScaffoldColumn(true)] to show a public property via metadata

4.3k views Asked by At

... it just doesn't work, at all. I've tried for days, with all different combinations of frick'n stuff and it won't budge.

There are certainly people out there who seem to be blogging about breezing through this sort of thing without seeing a glimpse of an issue saying things like "We all know that you can show public properties of extended EF classes by..." and "If you want to extend your data model to show a calculated field, simply..." - not so simple for me - arghghhhhghhhhhghh!!!

So, as per all of the typical examples, my EF partial class looks like this:

[DisplayColumn("Name")]
[MetadataType(typeof(SaleProduct_Metadata))]
public partial class SaleProduct
{        
    public string Test
    {
        get
        {
            return "blah";
        }
    }

    public class SaleProduct_Metadata
    {
        [ScaffoldColumn(true)] 
        public string Test;
    }
}  

My global.asax looks like this:

        MetaModel model = new MetaModel();
        model.RegisterContext(typeof(Sale.Models.SaleEntities), new ContextConfiguration() { ScaffoldAllTables = true });
        routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx")
        {
            Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
            Model = model
        });

And my List.aspx.cs looks like this:

public partial class List : System.Web.UI.Page
{
    protected MetaTable table;

    protected void Page_Init(object sender, EventArgs e)
    {
        //DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
        table = GridDataSource.GetTable();
        DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
        GridView1.ColumnsGenerator = new AdvancedFieldGenerator(table, true);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        table = GridDataSource.GetTable();
        Title = table.DisplayName;
        GridDataSource.Include = table.ForeignKeyColumnsNames;
        InsertHyperLink.NavigateUrl = table.GetActionPath(PageAction.Insert);

        // Disable various options if the table is readonly
        if (table.IsReadOnly)
        {
            GridView1.Columns[0].Visible = false;
            InsertHyperLink.Visible = false;
        }
    }

    protected void OnFilterSelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.PageIndex = 0;
    }
}

...I'm using a version of Dynamic Data Futures to get functionality like column ordering, better validation, etc, which is working fine. Have made a couple of adjustments to (e.g.) List.aspx.cs (shown above) and have changed the date formatting so as NZ style dates work on my US web-server. Other than that, everything's pretty standard, AFAIK.

My EF Models are housed in a separate assembly and am (obviously) extending some of the Entitys using partial classes. I just want to show two calculated bloody fields, but am having no success at all. I feel like I'm a new programmer bashing my head against a brick wall - none of the old tricks work. Programming shouldn't be this hard :-(

Someone, anyone, please help!!

Bernard.

2

There are 2 answers

0
AudioBubble On

When using EF, the namespaces of your partial class must match the namespace of the partial class generated for you. In Linq-2-SQL versions, the generated partial classes do not have a namespace, so it just works. http://forums.asp.net/t/1473192.aspx

0
Alex James On

I'm not sure but if you are using EF 3.5 SP1...

that would mean your classes derive from EntityObject, I suspect that Dynamic Data is special casing this situation and using a custom TypeDescriptionProvider that know how to get EF metadata from the ObjectContext, and then fakes the necessary Attributes.

This might be the cause of the problem, because maybe that TypeDescriptionProvider is only using the metadata for properties EF knows about. Which of course excludes your calculated properties.

I know this isn't an 'answer' but hopefully it points you in the right direction.

Alex