#眉標=ASP.NET 4.0、Dynamic Data、VS2010 #副標=新一代ASP.NET 4.0開發技術與應用(3) #大標=具備自動感知資料庫結構的Dynamic Data #作者=文/圖 董大偉 ===<反灰>============= model.RegisterContext(typeof(YourDataContextType), new ContextConfiguration() { ScaffoldAllTables = false }); 修改為: model.RegisterContext(typeof(crNorthWindEntities), new ContextConfiguration() { ScaffoldAllTables=true }); ================ ===<反灰>============= protected void Page_Load(object sender, EventArgs e) { //MetaModel.Default.VisibleTables傳回所有可用的Tables  System.Collections.IList visibleTables = MetaModel.Default.VisibleTables;  if (visibleTables.Count == 0)  { (…略…)  } //進行DataBinding  Menu1.DataSource = visibleTables;  Menu1.DataBind(); } ================ ===<反灰>============= http://localhost:64672/客戶/List.aspx //表列客戶資料 http://localhost:64672/客戶/Insert.aspx //新增客戶資料 http://localhost:64672/客戶/Details.aspx //顯示客戶資料Details http://localhost:64672/客戶/Edit.aspx //編輯客戶資料 ================ ===<反灰>============= http://localhost:64672/產品資料/List.aspx //表列產品資料 http://localhost:64672/產品資料/Insert.aspx //新增產品資料 http://localhost:64672/產品資料/Details.aspx //顯示產品資料Details http://localhost:64672/產品資料/Edit.aspx //編輯產品資料 ================ ===<反灰>============= routes.Add(new DynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = model }); ================ ===<反灰>============= protected void Page_Init(object sender, EventArgs e) { DynamicDataManager1.RegisterControl(GridView1, 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; } } ================ ===<反灰>============= (...略...)    (...略...) (...略...) ================ ===<反灰>============= //DynamicDataManager會嘗試將EntityDataSource繫結的資料表假定為這邊指定的Entities model.RegisterContext(typeof(crNorthWindEntities), new ContextConfiguration() { ScaffoldAllTables=true }); //資料表則自動假定為DynamicDataRoute中指定的{table}參數 routes.Add(new DynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = model }); ================ ===<反灰>============= [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)] [DataMemberAttribute()] [System.ComponentModel.DataAnnotations.UIHint("MyUITest")] public Nullable 送貨日期 { get { return _送貨日期; } set { On送貨日期Changing(value); ReportPropertyChanging("送貨日期"); _送貨日期= StructuralObject.SetValidValue(value); ReportPropertyChanged("送貨日期"); On送貨日期Changed(); } } ================ ===<反灰>============= public partial class MyUITest : System.Web.DynamicData.FieldTemplateUserControl { protected void Page_Load(object sender, EventArgs e) { (…略…) } } ================ ===<反灰>============= public partial class WebForm1 : System.Web.UI.Page { //建立LinqToSql NorthwindDataContext NorthwindDataContext db = new NorthwindDataContext(); protected void Page_Load(object sender, EventArgs e) { //設定自動產生編輯,新增鈕,並將預設模式指定為'編輯' this.DetailsView1.AutoGenerateEditButton = true; this.DetailsView1.AutoGenerateInsertButton = true; this.DetailsView1.DefaultMode = DetailsViewMode.Edit; //設定自動產生欄位 this.DetailsView1.AutoGenerateRows = true; //資料來源設為LinqToSql資料來源的Customers資料表 this.DetailsView1.DataSource = db.Customers; //讓DetailsView1支援EnableDynamicData(參考型別為Linq To Sql Data Model中的Customers) this.DetailsView1.EnableDynamicData(typeof(Customers)); //進行DataBind this.DetailsView1.DataBind(); } } ================ ===<反灰>============= //描述其MetadataType為CustomerMetadata [MetadataType(typeof(CustomerMetadata))] public partial class Customers //Entity 'Customers' 的partial class { } //實際的DataAnnotations修飾字建立在CustomerMetadata上 //用以修飾Customers中的欄位 public partial class CustomerMetadata { [Display(Name = "地址", Order = 5)] //Name是欄位標題文字, Order則是排列順序 public string Address; [Display(Name = "客戶編號", Order = 1)] [ReadOnly(true)] //ReadOnly描述該欄位為唯讀 public string CustomerID; [Display(Name = "客戶名稱", Order = 2)] [Required(ErrorMessage = "此為必要欄位")] //Required(…)指出該欄位為必要欄位 public string CompanyName; [Display(Name = "聯絡人名稱", Order = 3)] public string ContactName; [Display(Name = "頭銜", Order = 4)] public string ContactTitle; [Display(Name = "城市", Order = 7)] public string City; [Display(Name = "區域", Order = 6)] public string Region; [Display(Name = "郵遞區號", Order = 8)] public string PostalCode; [Display(Name = "國家", Order = 9)] public string Country; [Display(Name = "電話號碼", Order = 10)] public string Phone; [Display(Name = "傳真號碼", Order = 11)] public string Fax; } ================ ===<反灰>============= [DataType(DataType.EmailAddress)] public object HomeEmail { get; set; } [DataType(DataType.Url)] public object Website { get; set; } ================