2

I'm creating a WPF application that uses a LocalDB instance (which is provided by the ClickOnce installer).

The program uses a database to store userdata. When the application is deployed for the very first time, I want to create a LocalDB where some data is inserted upon initialization.

When I later provide an update to the program (which may include schema changes), I do not want to lose any userdata.

I'm using EF Code-First and this is my DbContext:

public class MyContext : DbContext
{
    public DbSet<Stuff> Premises { get; set; }
    public DbSet<Person> Persons { get; set; }


    private static MyContext _Current;

    public static MyContext Current
    {
        get
        {
            if (_Current == null)
            {
                _Current = new MyContext();
            }
            return _Current;
        }
    }

    protected MyContext()
    {
        //Some data to insert on the first time
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Stuff>().HasMany(p => p.Persons).WithRequired(m => m.Stuff);
    }
}

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="MyContext"
    connectionString="data source=(LocalDB)\mssqllocaldb;Integrated Security=True"
    providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Am I correct in thinking that adding my development .mdf file will always overwrite the userdata when I update via ClickOnce?

How can I let EF create the schema for the first time, update when there's changes, and never lose any userdata?

Benjamin Diele
  • 1,177
  • 1
  • 10
  • 26
  • `if(REQUIRED) UPDATE_DATABASE_RIGHT_NOW();` – Xi Sigma Mar 28 '15 at 18:46
  • @decoherence But how do I make sure that the database is created the first time, on the client's computer, without providing the .mdf file? – Benjamin Diele Mar 28 '15 at 18:48
  • that line was a joke...but are you sure that your database schema will never change as you roll updates? , because if the data changes you will need to apply the changes to the scema manually – Xi Sigma Mar 28 '15 at 18:52
  • @decoherence It will change, but I'll need to look up EF Migrations for that part. The first problem is that I don't want to overwrite the database file when I deploy an update. – Benjamin Diele Mar 28 '15 at 18:53
  • maybe version tolerant xml serialization is a better option that way you get more control and less headache – Xi Sigma Mar 28 '15 at 19:02

0 Answers0