Jay
Jay

Reputation: 723

Its good to use [NotMapped] in side EF code first Model

In My Case, I have a question about in my model I need one extra field like this

public class PassportStamp
    {
        [Key]
        public int Id { get; set; }
        [MaxLength(10, ErrorMessage = "BloggerName must be 10 characters or  less"), MinLength(5)]
        public string BloggerName { get; set; }
        [Required]
        public string Title { get; set; }
        [NotMapped]
        public string BlogCode
        {
            get
            {
                return Title.Substring(0, 1) + ":" + BloggerName.Substring(0, 1);
            }
        }
    }

Can I use this way or can I create a separate view model for this field and also can I use data Data Annotations at this way or create a separate view model for that

Upvotes: 1

Views: 113

Answers (1)

Vivek Nuna
Vivek Nuna

Reputation: 1

You are right, you can use [NotMapped] in your case. [NotMapped] is useful in those scenarios where you don't want to create a table column, but you want to use it with some custom properties.

e.g. In your model class Student, there are fields FirstName and LastName. If you client needs FullName of Student object. Then you can simply use [NotMapped] in this case. Because there is no use to create a separate column for this in the table. And you don't need to write a custom logic while using FullName throughout the application.

Upvotes: 1

Related Questions