Share via

NotMapped atribute .NET 5.0

Andrej Čačala 6 Reputation points
2020-12-16T22:34:24.903+00:00

I´m trying to use data anotation NOTMAPPED in .NET 5 and when i run aplication i get error that collumn can not find in db. In NET framework what i used before on this project was working fine

Developer technologies | ASP.NET Core | Other
0 comments No comments

1 answer

Sort by: Most helpful
  1. Michael Wang-MSFT 1,066 Reputation points
    2020-12-17T07:55:51.647+00:00

    Hi, @Andrej Čačala ,

    The NotMapped attribute is still supported in .NET 5. It can be applied to properties of an entity class for which we do not want to create corresponding columns in the database. By default, EF creates a column for each property (must have get; & set;) in an entity class. The [NotMapped] attribute overrides this default convention. You can apply the [NotMapped] attribute on one or more properties for which you do NOT want to create a corresponding column in a database table.

    public class Student  
        {  
            public int Id { get; set; }  
            public string Name { get; set; }  
      
            [NotMapped]  
            public int Age { get; set; }  
        }  
    

    The above model maps to the database as shown below.

    48989-image.png

    Alternatively you could try Ignore() :

    protected override void OnModelCreating(DbModelBuilder modelBuilder)  
    {  
       modelBuilder.Entity<Student>().Ignore(m => m.Age );  
       base.OnModelCreating(modelBuilder);  
    }  
    

    If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Michael Wang

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.