Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore a property when using Entity Framework Code First [duplicate]

Entity Framework Code First will auto-create a table in the database base based on the Model.

Is there an attribute that will avoid this?

like image 231
Dozer Avatar asked May 13 '12 14:05

Dozer


People also ask

How do you ignore an entity EF core?

The Entity Framework Core Fluent API provides two Ignore methods. One belongs to the ModelBuilder class and is used to specify that the entity should not be mapped to a database table. The other Ignore method is available on the EntityTypeBuilder class and enables you to exclude individual properties from mapping.

What is not mapped in C#?

The NotMapped attribute is used to specify that an entity or property is not to be mapped to a table or column in the database. In the following example, the AuditLog class will not be mapped to a table in the database: public class Contact. {


2 Answers

Add the [System.ComponentModel.DataAnnotations.Schema.NotMapped] attribute to the property.

like image 117
SLaks Avatar answered Oct 01 '22 22:10

SLaks


Per the accepted answer and similar question/answer, in addition to [NotMapped] you can also specify it using the Fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {    modelBuilder.Entity<TheModelAffected>().Ignore(t => t.TheIgnoredProperty);    base.OnModelCreating(modelBuilder); } 
like image 29
drzaus Avatar answered Oct 01 '22 20:10

drzaus