Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework to read a column but prevent it being updated

Given a database table with a column that contains historic data but that is no longer populated, is there a way in Entity Framework to read the column but prevent it being updated when using the same model object?

For example I have an object

public class MyObject
{
    public string CurrentDataColumnName { get; set; }
    public string HistoricDataColumnName { get; set; }
}

From the documentation I don’t believe I can do either of the following, because this will stop EF reading the data as well as persisting it.

(1) Decorate the HistoricDataColumnName property with the following attribute

[NotMapped]

(2) Add the following to my EntityTypeConfiguration for MyObject

Ignore(x => x.HistoricDataColumnName)
like image 842
Greg Trevellick Avatar asked Jun 11 '15 09:06

Greg Trevellick


People also ask

How do I stop Entity Framework tracking?

In Entity Framework, change tracking is enabled by default. You can also disable change tracking by setting the AutoDetectChangesEnabled property of DbContext to false. If this property is set to true then the Entity Framework maintains the state of entities.

Why you should not use Entity Framework?

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 is the difference between EF6 and EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.


2 Answers

You can mark the column as computed to prevent Entity Framework from updating / inserting into that column.

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string HistoricDataColumnName { get; set; }

DatabaseGenerated

An important database features is the ability to have computed properties. If you're mapping your Code First classes to tables that contain computed columns, you don't want Entity Framework to try to update those columns. But you do want EF to return those values from the database after you've inserted or updated data. You can use the DatabaseGenerated annotation to flag those properties in your class along with the Computed enum. Other enums are None and Identity.

like image 134
Aducci Avatar answered Oct 26 '22 13:10

Aducci


You can simply use IsModified to check whether a specific entity property was modified or not and by this way you can still Read,Insert and Delete data:

var item = context.MyObjects.Find(id);
item.CurrentDataColumnName = "ChangedCurrentDataColumnName";
item.HistoricDataColumnName = "ChangedHistoricDataColumnName";
context.Entry(item).Property(c => c.HistoricDataColumnName).IsModified = false;
context.SaveChanges();

By using IsModified = false you are excluding the HistoricDataColumnName property from updating, so the HistoricDataColumnName column will not be updated in the database but other properties will be updated.

Setting this value to false for a modified property will revert the change by setting the current value to the original value. If the result is that no properties of the entity are marked as modified, then the entity will be marked as Unchanged. Setting this value to false for properties of Added, Unchanged, or Deleted entities is a no-op.

Check the following answer as a supplementary explanation. It might be helpful also:

https://stackoverflow.com/a/13503683/2946329

like image 38
Salah Akbari Avatar answered Oct 26 '22 12:10

Salah Akbari