I have a DBContext object which contains the definitions of all my database objects. When I ran the application for the first time, it created the database. This is now in production.
I now have needed to make model changes in DEV and so will need to use Code First Migrations to generate a SQL script that makes the required schema changes in prod when I release.
As I understood it, I could add a new migration with the add-migration command and it would compare what is in my existing database with the model definition and generate a script for me that would allow me to update the database. However when I run the add-migration command, it generates a migration for me that is a full database create, not simply the new objects.
My database objects are in a separate class library, I have opened up package manager console, selected the default project to be my DB class library.
Under Configuration.cs I have
internal sealed class Configuration : DbMigrationsConfiguration<Centrica.EMT.Database.Context.EmtDataContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
ContextKey = "Centrica.EMT.Database.Context.EmtDataContext";
}
And in the app.config of that class library I have
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<connectionStrings>
<add name="Centrica.EMT.Database.Context.EMTDataContext" connectionString="Initial Catalog=EMTProd;Data Source=WYCVWWEBD016\COMPUTECLUSTER;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
yet it doesn't seem to find that database and generate an update script, it just creates a brand new full database creation script for me when I create a new migration.
Am I not able to create an update script? Did I have to have an existing migration performed initially as a baseline? Have I configured it wrong for how to find the existing database?
Updated
public class EmtDataContext : DbContext
{
public EmtDataContext()
{
//todoSystem.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists<EmtDataContext>());
var objectContextAdapter = this as IObjectContextAdapter;
var objectContext = objectContextAdapter.ObjectContext;
objectContext.CommandTimeout = Database.Connection.ConnectionTimeout;
}