Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore properties in data model while keeping them in EF Core migrations

I am creating a greenfield application that uses EF Core which must talk to a legacy database. I want EF to ignore some of the columns in the database because they will eventually be deprecated and I don't want them in the new entity model. I can't remove them yet as the legacy system still relies on them.

For an unwanted database column called DeprecatedFeature, I want to do something like:

modelBuilder.Entity<MyEntity>(entity => {
   entity.HasKey(t => t.Id);
   entity.ToTable("MyEntity");
   entity.ColumnIgnore("DeprecatedFeature"); // <-- this is what I want to do
})

Right now, the best I can do is include the property and mark it as obsolete:

public class MyEntity 
{
    public int Id { get; set; }

    [Obsolete("Deprecated in latest version")]
    public string DeprecatedFeature { get; set; }
}

But that means I can't turn on "warnings as errors". I still need to run migrations on my database.

Similar questions: EF 4.x, EF Core skip column on load, Using EF Designer/EDMX and duplicate

Edit

I can see by the answers that there is some confusion about my question:

NotMapped is NOT the answer

NotMapped is used when you have a property in your model that you don't want in the database. My problem is the other way around. I have a column in my database that I don't want in my model.

like image 603
Dr Rob Lang Avatar asked Nov 01 '17 15:11

Dr Rob Lang


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.

When should you not use Efcore?

One of the biggest reasons not to use Entity Framework Core is that your application needs the fastest possible data access. Some applications do a lot of heavy data operations with very high-performance demands, but usually business applications don't have that high of a performance demand.

What are the different types of properties supported in Entity Framework?

An Entity can include two types of properties: Scalar Properties and Navigation Properties. Scalar Property: The type of primitive property is called scalar properties. Each scalar property maps to a column in the database table which stores the real data.


4 Answers

You have two alternatives:

  1. Using NotMappedAttribute:

    public class MyEntity
    {
         public int Id { get; set; }    
         [NotMapped]
         public string DeprecatedFeature { get; set; }
    }
    
  2. Using FluentAPI:

    modelBuilder.Entity<MyEntity>().Ignore(c => c.DeprecatedFeature);
    
like image 105
Yared Avatar answered Sep 28 '22 01:09

Yared


Just don't include that property in your entity class. EntityFramework should just ignore it then.

public class MyEntity 
{
    public int Id { get; set; }

    // Remove this property
    //public string DeprecatedFeature { get; set; }
}

You should be able to access this entity from the database without any problems, and your application code won't have access to the deprecated property. If you need write to this table, the deprecated column will need to either be nullable or have a default value.


Edit:

You can create a shadow property like this:

entity.Property(typeof(string), "DeprecatedFeature");

This will let EF be aware of the property (and include it in migrations), but the property doesn't need to exist on the entity type.

like image 41
Entith Avatar answered Sep 27 '22 01:09

Entith


I didn't see an accepted answer to this question so here is mine. I believe that the thing called "shadow properties" is exactly what you need.

With a shadow property you can define a field in the table but don't add a property to your model class:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<MyEntity>()
        .Property<string>("DeprecatedFeature");
}
like image 3
Sergiy Avatar answered Sep 26 '22 01:09

Sergiy


I have similar requirement, where I need the property in code but db populates the field. I used the following attribute under System.ComponentModel.DataAnnotations.Schema namespace

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

This way, the property is mapped but is not populated when using entityframework.

like image 1
salman_sali Avatar answered Sep 28 '22 01:09

salman_sali