可以通過NotMappedAttribute標記模型某個屬性可以使該屬性不必映射到數據庫。
public class Unicorn { public int Id { get; set; } [NotMapped] public string Name { get; set; } [Timestamp] public byte[] Version { get; set; } public int PrincessId { get; set; } // FK for Princess reference public virtual Princess Princess { get; set; } }
NotMapped無效的時候,在DbContext的OnModelCreating方法重載中實現
protected override void OnModelCreating(DbModelBuilder modelBuilder) { //不映射到數據庫中 modelBuilder.Entity<BlogArticle>().Ignore(p => p.Title); }