2012-04-09 41 views
0

相對於另一個SO後我需要更新我的模型,以便'新'列ActiveBool不會嘗試匹配數據庫表。阻止模型和數據庫之間的匹配MVC 3和實體框架

型號:

public class StatusList 
    { 
     [Key] 
     public int StatusID { get; set; } 

     [Required]   
     public byte Active { get; set; } 

     //I want this column to be ignored by Entity Framework 
     public bool ActiveBool 
     { 
      get { return Active > 0; } 
      set { Active = value ? Convert.ToByte(1) : Convert.ToByte(0); } 

     } 
    } 

是否有可以使用的DataAnnotation?

+0

提示:NotMapped] – 2012-04-09 20:21:58

+1

[NotMapped] ..... – 2012-04-09 20:32:24

回答

0

您需要使用[NotMapped]註釋:

public class StatusList 
    { 
     [Key] 
     public int StatusID { get; set; } 

     [Required]   
     public byte Active { get; set; } 

     //I want this column to be ignored by Entity Framework so I add [NotMapped] 
     [NotMapped] 
     public bool ActiveBool 
     { 
      get { return Active > 0; } 
      set { Active = value ? Convert.ToByte(1) : Convert.ToByte(0); } 

     } 
    }