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


I've been searching on Google for three days and haven't been able to come up with a useful response.

I'm going to start with this AMAZING tutorial:

This project is being expanded so that the "Academic Year" might be a lookup to a different database table rather than an enum. I'm surprised at how tough it is to locate a case study 4.5, VS 2013, EF 6, and dynamic fields to demonstrate this.

So, here is what I did:

1) The database now contains a new table called "Years" (YearID tinyint PK, YearName varchar). Added the potential values to that table.

2) I updated the Students field's name from Year to YearID to tinyint for consistency's sake and made a linkage between the Students. the Years and YearID. YearID

3) I added the new table to the SchoolContext.

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

4) The Year class was added as follows:

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

5) I deleted the student class's references to the academic year and replaced them with the following text:

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

6) Modified the form's field to display the recently linked YearID:

<asp:DynamicField DataField="YearID" />

(At last) the issue I'm unable to figure out how to change YearID such that a dropdown list appears instead of a textbox. When it was an enum, it worked perfectly, but as a lookup, it was useless. It had the following annotation when it was specified as an enum:

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

Does the Dynamic control need to have a similar annotation to inform it to obtain its data from the Years table? As far as I can determine, the entire situation is quite strange and unrecorded.

I modified the DataField on the Dymanic Field to "Year" instead of "YearID" since I read somewhere that you should use the name of the connected field rather than the anything whatever. When I do that, "System.Data.Entity.DynamicProxies.Year 9D4E99..." renders on the control.

Naturally, the default setting in my ForeignKey Edit.ascx file—showing a dropdownlist—remains in place.

I'm hoping I'm totally missing something obvious here.


2/14/2014 8:19:50 PM

Accepted Answer

Installing "Dynamic Data provider for EF6" is required for entity framework 6 (see [">Dynamic Data provider for EF6"] .aspx] [1]

Open the Package-Manager console and execute:

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

For dynamic data, register the model in global.asax.cs

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 });

You must utilize the navigational field in Students.aspx rather than the ID:

<asp:DynamicField DataField="Year" />

To student.aspx.cs, add this.

using System.Web.DynamicData;

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

This method must be added to each page that has a gridview or formview with 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