1

In my team we C# developer do not have schema modification access to Database directly. There is a separate team which adds/alter tables for us. When a new requirement comes which needs a DB schema change they add the column/table and give us the details so we update our model class.

Following is the DataContext

public class FamilyDataContext : DbContext
{
    public FamilyDataContext() : base("FamilyDataContext")
    {
    }

    public DbSet<ResidentInformation> ResidentInformation { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

Problem: I am getting an error while trying to save data in this table.

The model backing the 'FamilyDataContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

What I want is, if there is a mismatch in Model definition in C# code and Db schema, give an error so that it can be fixed manually either by dev or by DBA; rather than trying to upgrade the Database automatically. I do not want any automatic migration.

Is there anyway to achieve this please?

Maheep
  • 5,539
  • 3
  • 28
  • 47

1 Answers1

7

You could use the null initializer:

Database.SetInitializer(new NullDatabaseInitializer<ApplicationDbContext>());

but that will not tell you your model is out of sync. To do that, you will need to make a custom initializer, then do a check like

if (!context.Database.CompatibleWithModel(true))
{
    // handle out of sync
}

Here is a walkthrough on custom initializers: http://www.codeguru.com/csharp/article.php/c19999/Understanding-Database-Initializers-in-Entity-Framework-Code-First.htm

or a more concise example: https://coding.abel.nu/2012/03/prevent-ef-migrations-from-creating-or-changing-the-database/

Steve Greene
  • 12,029
  • 1
  • 33
  • 54