2012-12-20 167 views
1

我試圖建立一個與實體框架代碼第一個數據庫與ASP.Net MVC 4實體框架MVC 4類問題

我是新來的MVC &實體框架和我與如何掙扎設計我的類對象。

我想各位類像下面的一個,具有AddressInformation類的數據屬性: -

public class Member 
    { 
     public virtual int MemberID { get; set; } 
     public virtual string Forename { get; set; } 
     public virtual string Surname { get; set; } 
     public virtual int age { get; set; } 
     public virtual AddressInformation Address { get; set; } 
     public virtual string EmailAddress { get; set; } 
     public virtual string HomePhoneNumber { get; set; } 
     public virtual string MobileNumber { get; set; } 
    } 



public class AddressInformation 
    { 
     public virtual int MemberID { get; set; } 
     public virtual string HouseNoName { get; set; } 
     public virtual string StreetName { get; set; } 
     public virtual string Town { get; set; } 
     public virtual string County { get; set; } 
     public virtual string PostCode { get; set; } 
     public virtual string Country { get; set; } 
    } 

我也有另一個類,從的DbContext繼承: -

public class CentralDataStore :DbContext 
    { 
     public DbSet<Member> Members { get; set; } 
     public DbSet<AddressInformation> AddressInfo { get; set; } 
    } 

當我添加控制器時,我沒有獲得輸入AddressInformation的信息,只有成員信息已填充到我的視圖中。

任何人都建議最好的方法來攻擊?正如我所說,我是MVC的新手。

+0

您是否嘗試添加公共虛擬詮釋ID {get;組; } Addressinformtion類? –

回答

2

你不需要做所有屬性的虛擬,僅用於的那些導航。你需要使用Fluency API建立Member和AddressInformation之間的關係。此外,您的主鍵需要命名爲Id或使用屬性或流利API來指定它是主鍵。您還缺少將成員映射到AddressInformation的id。這是你的類定義應該是什麼樣子。

public class Member 
{ 
    public int ID { get; set; } 
    public string Forename { get; set; } 
    public string Surname { get; set; } 
    public int age { get; set; } 
    public virtual int AddressId { get; set; } 
    public virtual AddressInformation Address { get; set; } 
    public string EmailAddress { get; set; } 
    public string HomePhoneNumber { get; set; } 
    public string MobileNumber { get; set; } 
} 

public class AddressInformation 
{ 
    public int ID { get; set; } 
    public string HouseNoName { get; set; } 
    public string StreetName { get; set; } 
    public string Town { get; set; } 
    public string County { get; set; } 
    public string PostCode { get; set; } 
    public string Country { get; set; } 
} 

注意我添加了AddressId屬性以提供映射到AddressInformation對象/表。像這樣在Fluency API中配置關係。

public class MemberConfig : EntityTypeConfiguration<Member> 
{ 
    internal MemberConfig() 
    { 
     this.HasKey(m => m.ID); 
     this.HasRequired(m => m.Address) 
      .WithRequiredDependent(a => a.ID) 
      .HasForeignKey(m => m.AddressId); 

    } 
} 

通過設置外鍵關係EF將自動將AddresssInformation加載到Member對象中。

+0

感謝您的光明解釋。 – Derek

2

據我所知,生成視圖的標準模板並沒有實現嵌套對象的輸入字段。但是可以選擇擴展MVC應用程序的標準模板,如link。在那裏你可以爲嵌套類添加輸入字段的生成,如果你是類似於T4模板的話。

2

您必須使用該模板要小心,你可以很容易地得到一個計算器

特別是在使用實體框架時,當你有導航性能的兩個實體指向對方

的默認Object模板可防止遞歸到特定深度以防止無限循環。我不喜歡這個,所以我寫了我自己:

/Views/Shared/object.cshtml

@model object 

@using System.Text; 
@using System.Data; 

@{ 
    ViewDataDictionary viewData = Html.ViewContext.ViewData; 
    TemplateInfo templateInfo = viewData.TemplateInfo; 
    ModelMetadata modelMetadata = viewData.ModelMetadata; 

    System.Text.StringBuilder builder = new StringBuilder(); 

    string result; 

    // DDB #224751 
    if (templateInfo.TemplateDepth > 2) 
    { 
    result = modelMetadata.Model == null ? modelMetadata.NullDisplayText 
             : modelMetadata.SimpleDisplayText; 
    } 

    foreach (ModelMetadata propertyMetadata in modelMetadata.Properties 
    .Where(pm => pm.ShowForEdit 
      && pm.ModelType != typeof(System.Data.EntityState) 
      && !templateInfo.Visited(pm))) 
    { 
     builder.Append(Html.Editor(propertyMetadata.PropertyName).ToHtmlString()); 
    } 

    result = builder.ToString(); 
} 

@Html.Raw(result)