2012-11-29 15 views
1

我是新來的MVC,並試圖找出如何設置部分類的默認值。我一直在尋找2天,並沒有任何工作。 Here是一個假設的解決方案,但它不適合我。我也嘗試了[DefaultValue(10)]數據註釋。MVC 4 EF5數據庫首先設置部分類中的默認值

這裏是自動生成的從EDMX文件

​​

這裏是我創建添加註釋我偏類創建部分類。

namespace OTIS.Models.Admin 
{ 
    [MetadataType(typeof(CompanyMD))] 
    public partial class Company 
    { 

     //public Company() 
     //{ 
     // //private System.DateTime _currentDateTime = DateTime.Now; 
     // ////Set Default Values 
     // //CreatedByID = (int)Membership.GetUser().ProviderUserKey; 
     // //CreatedOn = _currentDateTime; 
     // //ModifiedBy = (int)Membership.GetUser().ProviderUserKey; 
     // //ModifiedOn = _currentDateTime; 
     //} 

     public string FullAddress 
     { 
      get 
      { 
       return this.City + ", " + this.State + " " + this.PostalCode; 
      } 
     } 

     public class CompanyMD 
     { 
      private System.DateTime _currentDateTime = DateTime.Now; 
      private int _currentUser = (int)Membership.GetUser().ProviderUserKey; 


      [Display(Name = "Company ID")] 
      public int CompanyID { get; set; } 

      [Required] 
      [Display(Name = "Company Name")] 
      public string CompanyName { get; set; } 

      [Display(Name = "Address")] 
      public string Address1 { get; set; } 

      [Display(Name = "Address 2")] 
      public string Address2 { get; set; } 

      public string City { get; set; } 
      public string State { get; set; } 

      [Display(Name = "Zip")] 
      public string PostalCode { get; set; } 

      [Display(Name = "Address")] 
      public string FullAddress { get; set; } 

      [Display(Name = "Material Margin")] 
      public decimal ItemMargin { get; set; } 

      [Display(Name = "Overtime Margin")] 
      public decimal ServicesMargin { get; set; } 

      [Display(Name = "Invoice Hour Increment")] 
      public decimal InvoiceTimeIncrement { get; set; } 

      private decimal _cashDiscountPct; 

      [Display(Name = "Cash Discount %")] 
      [DisplayFormat(DataFormatString = "{0:P2}")] 
      public decimal CashDiscountPct 
      { 
       get { return _cashDiscountPct; } 
       set { _cashDiscountPct = value/100; } 
      } 

      [Display(Name = "Base Hourly Rate ($/Hr)")] 
      [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = true)] 
      public decimal BaseServiceHourlyRate { get; set; } 

      [Display(Name = "Rush Premium ($/Hr)")] 
      [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = true)] 
      public decimal HourlyPremiumRush { get; set; } 

      [Display(Name = "Late Premium ($/Hr)")] 
      [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = true)] 
      [DefaultValue(75)] 
      public decimal HourlyPremiumLate { get; set; } 

      [Display(Name = "Cust Material Premium ($/Hr)")] 
      [DataType(DataType.Currency), DisplayFormat(DataFormatString = "{0:C2}", ApplyFormatInEditMode = true)] 
      public decimal HourlyPremiumCustomerMaterial { get; set; } 

      [Display(Name = "Created By")] 
      public int CreatedByID { get; set; } 
      //{ 
      // get { return _currentUser; } 
      // set { _currentUser = value; } 
      //} 

      [Display(Name = "Created On")] 
      [DatabaseGenerated(DatabaseGeneratedOption.Computed)] 
      //[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
      public System.DateTime CreatedOn 
      { 
       get { return _currentDateTime; } 
       set { _currentDateTime = value; } 
      } 

      [Display(Name = "Modified By")] 
      public int ModifiedBy { get; set; } 
      //{ 
      // get { return _currentUser; } 
      // set { _currentUser = value; } 
      //} 

      [Display(Name = "Modified On")] 
      public System.DateTime ModifiedOn 
      { 
       get { return _currentDateTime; } 
       set { _currentDateTime = value; } 
      } 

     } 

    } 
} 

然後在我的控制,我實例化類的新實例初始化它,但我設置的值不獲取設置。

// 
     // GET: /Company/Create 

     public ActionResult Create() 
     { 
      ViewBag.CreatedByID = new SelectList(db.UserProfiles, "UserId", "UserName"); 
      ViewBag.ModifiedBy = new SelectList(db.UserProfiles, "UserId", "UserName"); 
      Company newCompany = new Company(); 
      return View(newCompany); 
     } 
+0

注意:如果我對公共公司()行取消註釋,則會出現錯誤:類型'OTIS.Models.Admin.Company'已定義名爲'Company'的成員具有相同的參數類型 –

回答

0

我發現我需要在GetNew()方法的Repository類中處理這個問題,該方法將填充類的新實例的默認值。

3

對不起,這是太晚了,但我自己也解決了類似的情況。

我認爲問題在於如何引用部分類。它應該是沒有代碼的部分類的空引用。 EF使用此「聲明」將您的部分類鏈接到您的元數據類。所以,你的元數據類應該看起來像這樣:

namespace OTIS.Models.Admin 
{ 
    [MetadataType(typeof(CompanyMD))] 
    public partial class Company 
    {} // <-- note the close bracket! 

    public class CompanyMD 
    { 
     private System.DateTime _currentDateTime = DateTime.Now; 
     private int _currentUser = (int)Membership.GetUser().ProviderUserKey; 

     public string FullAddress 
     { 
      get 
      { 
       return this.City + ", " + this.State + " " + this.PostalCode; 
      } 
     } 

     [Display(Name = "Company ID")] 
     public int CompanyID { get; set; } 
     [Required] 
     [Display(Name = "Company Name")] 
     public string CompanyName { get; set; } 

     // ....etc.... removed for brevity 

    } // close metadata class 
} // close namespace 

希望這有助於!

+0

感謝您的支持repsonse jpg0002,但我不想要一個涉及EF自動生成的類的更改的解決方案,因爲它肯定會被重寫。我最終不得不在我的存儲庫類中處理它。 –

+0

@Chad Richardson我同意,你不應該改變EF生成的類。 正確的解決方案是增加以下的元數據類: '[MetadataType(typeof運算(CompanyMetadata))]' '公共部分類Company' '{}' – JohnG

+0

我編輯我的回答,以顯示更比改變自動生成的文件更好的方法來做到這一點。 – JohnG