How do I add columns to SQLLocalDB schema (modelled using EF6 database first) without users losing data when a new version of the application is publi

  • Thread starter Thread starter Mike Whalley
  • Start date Start date
M

Mike Whalley

Guest
I have a c# .NET application with a SQLLocalDB database first model built using ADO.NET and Entity Framework 6. I have added columns to one of the tables using SQL Server Management Studio and then used 'Update model from database' to propagate the changes into my model. When doing this, data on the development PC is unaffected and incorporates the new columns. However, when a different user runs the new version of the application, their existing datafile (.mdf) won't recognise the new columns and crashes with the error:

System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'VAT_long_description'
making the user's existing data in that table unreachable. I appreciate that Code First gives the ability to migrate data, but this facility appears not to be available when building the EF model using database first.
My connectionString is:

<add name="PMMEntities" connectionString="metadata=res://*/PMMData.csdl|res://*/PMMData.ssdl|res://*/PMMData.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\v11.0; AttachDBFilename=|DataDirectory|\PMM.mdf; initial catalog=PMM;Integrated security=True;MultipleActiveResultSets=True; App=EntityFramework&quot;"
providerName="System.Data.EntityClient" />
I need to ensure that when a user installs a new version, their .mdf datafile will adapt to the new database schema without loss of data. It seems the only way to do this is through running a conversion or migration method on startup if the app throws the “Invalid column name” SQL exception. Where can I find the code (or a NuGet package) that will do this?
Mike Whalley

Continue reading...
 
Back
Top