Finding a foreign key Dropdownlist EF 6

I work for 3 days in Google and cannot find a useful answer, I hope someone can help me.

I start with this EXCELLENT tutorial:

http://www.asp.net/web-forms/tutorials/data-access/model-binding/retrieving-data

I am trying to expand this project so that the "Academic Year" becomes a search for another table in the database, and not an enumeration. I am surprised how hard it is to find an example illustrating this using .net 4.5, VS 2013, EF 6 and dynamic fields.

So here is what I did:

1) Added a new table called "Years" in the database (YearID tinyint PK, YearName varchar). This table is entered with possible values.

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

3) Modified SchoolContext to include a new table:

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

4) The Year class is added 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 study material from the Student class and replaced these lines:

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

6) Changed the field in the form to show the recently associated YearID:

<asp:DynamicField DataField="YearID" />

(Finally) problem: YearID shows as a text field, not a drop-down list, and I cannot figure out how to fix this. It worked fine when it was an enumeration, but was not as good as a search. When it was defined as an enumeration, it had this annotation:

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

Is there a similar annotation needed to tell the dynamic control the data from the Years table? As far as I know, all this is very mysterious and undocumented.

- , , , DataField Dymanic "Year", "YearID". , "System.Data.Entity.DynamicProxies.Year_9D4E99.........".

, ForeignKey_Edit.ascx , .

, - .

+3
1

framework 6 " EF6": [http://blogs.msdn.com/b/webdev/archive/2014/02/28/announcing-the-release-of -dynamic- -EntityDataSource-----6.aspx] [1]

Package-Manager :

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

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

Students.aspx insted ID:

<asp:DynamicField DataField="Year" />

student.aspx.cs

using System.Web.DynamicData;

protected void Page_Init()
    {
        MetaTable table = MetaTable.GetTable(typeof(Student));
        studentsGrid.SetMetaTable(table);
    }

gridview formview, .

+1

All Articles