Avoiding mapping property to table

Can anybody tell me about how I can avoid mapping some property from my model to table using code first approach?

Example:

public class Post
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "Posted")]
    public DateTime Created { get; set; }

    [Display(Name = "Modified")]
    public DateTime? Modified { get; set; }

    [Required]
    [MaxLength(256)]
    public string Title { get; set; }

    [Required]
    public string Body { get; set; }

    public int? UserId { get; set; }

    public string UserName { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }

    public virtual ICollection<Tag> Tags { get; set; } 
}

I want UserName do not map into table. It is possible or not in EF?

Answers


Add a

[NotMapped] 

attribute to the desired property:

public class Post
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "Posted")]
    public DateTime Created { get; set; }

    [Display(Name = "Modified")]
    public DateTime? Modified { get; set; }

    [Required]
    [MaxLength(256)]
    public string Title { get; set; }

    [Required]
    public string Body { get; set; }

    public int? UserId { get; set; }

    [NotMapped]
    public string UserName { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }

    public virtual ICollection<Tag> Tags { get; set; } 
}

Need Your Help

Add contraint that checks sum

sql database postgresql constraints

I'm trying to add a constraint to a table by checking that the sum of a value is &lt; 100.

jquery fadein problem

jquery

On my page, I put two divs: nav and contents.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.