Foreign Key Lookup Dropdownlist EF 6 drop-down-menu entity-framework entity-framework-6 webforms


I have been googling for 3 days, and can't find a usable answer, I'm hoping someone can help me.

I'm starting with this EXCELLENT tutorial:

I'm trying to extend this project to allow the "Academic Year" to be a lookup to another table in the database, rather than an enum. I am astonished how difficult it is to find an example that illustrates this using .net 4.5, VS 2013, EF 6 and Dynamic fields.

So, here's what I've done:

1) Added a new table called "Years" to the database (YearID tinyint PK, YearName varchar). Populated that table with the possible values.

2) Renamed the field in Students from Year to YearID for consistency, I changed it to tinyint, created a relationship between Students.YearID and Years.YearID

3) Changed the SchoolContext to include the new table:

public DbSet<Year> Years { get; set; }

4) Added the Year class, as follows:

public class Year
  public byte YearID { get; set; }
  public string YearName { get; set; }
  public virtual ICollection<Student> Students { get; set; }

5) Removed the academic year stuff from the Student class and replaced with these lines:

public byte YearID { get; set; }
public virtual Year Year { get; set; }

6) Changed the field on the form to show the newly linked YearID:

<asp:DynamicField DataField="YearID" />

(Finally) the problem: YearID shows as a textbox, rather than a dropdown list, and I cannot figure out how to fix this. It worked fine when it was an enum, but no good as a lookup. When it was defined as an enum, it had this annotation:

    [EnumDataType(typeof(AcademicYear)), Display(Name = "Academic Year")]
    public AcademicYear Year { get; set; }

Is there similar annotation required to tell the Dynamic control to get its data from the Years table? This whole thing is very mysterious and undocumented, as far as I can tell.

I read somewhere that you're supposed to use the name of the linked field, rather than the whatever whatever, so I changed the DataField on the Dymanic Field to "Year", rather than "YearID". When I do that, the control renders with "System.Data.Entity.DynamicProxies.Year_9D4E99.........".

Of course, my ForeignKey_Edit.ascx file remains at the default, which is to show a dropdownlist.

I hope I'm missing something super-obvious here.


2/14/2014 8:19:50 PM

Accepted Answer

For entity framework 6, you have to install "Dynamic Data provider for EF6": [][1]

In the Package-Manager console run:

Install-Package Microsoft.AspNet.DynamicData.EFProvider -Version 6.0.0

Register the model in global.asax.cs for dynamic data

using System.Web.DynamicData;

void Application_Start(object sender, EventArgs e)
        MetaModel DefaultModel = new MetaModel();
        DefaultModel.RegisterContext(new  Microsoft.AspNet.DynamicData.ModelProviders.EFDataModelProvider(
                                     () => new SchoolContext()),
                                     new ContextConfiguration { ScaffoldAllTables = false });

In Students.aspx you will need to use the navigational field insted of the ID:

<asp:DynamicField DataField="Year" />

Add this to student.aspx.cs

using System.Web.DynamicData;

protected void Page_Init()
        MetaTable table = MetaTable.GetTable(typeof(Student));

You will have to add this method to each page with gridview or formview that contains navigational fields.

3/26/2014 12:32:29 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow