Is it possible to make Entity Framework (Core) ignore missing columns when writing, to support backwards compatibility with old databases

1

We are using Entity Framework Core to access multiple versions of our database.

We would like to have a single tool be able to work with a range of versions of the database. But if a new column has been added to a table when we use EF to write to an older database that doesn't yet have the new column we get this exception:

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'SomeNewColumn'

We could have code that checked the version, and marked these columns to be ignored.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    if (databaseVersion < versionThatSupportsNewColumn)
        modelBuilder.Entity<ModelType>().Ignore(x => x.SomeNewColumn);
}

But is there a way to tell Entity Framework to just automatically ignore missing columns when doing updates rather than throwing the exception? So we don't have to go through adding every new column to get the backwards compatible support working.

(We're using EF core, but an answer for EF 6 would be equally interesting)

c#
.net
entity-framework
entity-framework-core
backwards-compatibility
asked on Stack Overflow Apr 19, 2021 by Simon P Stevens • edited Apr 19, 2021 by Simon P Stevens

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0