0

I have a Windows Program which should be deployed without writing to AppData during Installation, but i want a Sql CE Database in there.

So i have 2 Ideas: Deploying it to Programs and then Copying on first start up, or, what i would prefer, Coding everything.

So i found this Thread Deploying VS2010 Database Projects Without VSDBCMD? which basically tells me its a bad idea, but i don`t think the Answerer is allknowing and i have a better Idea. I found that Piece of Code somewhere:

string connStr = "Data Source = FooDatabase.sdf; Password = SomePassword";

if (File.Exists("FooDatabase.sdf"))
    File.Delete("FooDatabase.sdf");

SqlCeEngine engine = new SqlCeEngine(connStr);
engine.CreateDatabase();

using (var conn = new SqlCeConnection(connStr)) {
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "CREATE TABLE FooTable(col1 int, col2 ntext)";
    cmd.ExecuteNonQuery();

from here: Create SQLCE database programmatically

what i am asking is, if this works on any PC and what i have to do code to use Entity Framework. I was thinking of something like

public class FooContext : System.Data.Entity.DbContext 
{
    public FooContext() : base("Data Source = FooDatabase.sdf; Password = SomePassword") { }
    public DbSet<FooTable> FooTables { get; set; }
}

public class FooTable
{
    public int col1 { get; set; }
    public string col2 { get; set; }
}

but i just dont get it to work.

might be that only the connectionstring is wrong, but how do i avoid using it or how does the right one look like?

Help plz?

Community
  • 1
  • 1
efkah
  • 1,628
  • 16
  • 30

1 Answers1

0

Try to use this in your startup logic

private static void InitializeDB()
{
   const string connectionString = "Data Source = FooDatabase.sdf; Password = 123";
   Database.DefaultConnectionFactory = new
      SqlCeConnectionFactory("System.Data.SqlServerCe.4.0", "", connectionString);
}

take a look at this post

Iprog
  • 7
  • 1