none
DataContext Change

    Question

  • Is there a way to add a new column to a table once the database is created?  I have an application that has a table with col1, col2 and col3 and the database this is created when the application is first launched.  My questions is how do I add another column col4 once the database is already created???

    Tuesday, January 17, 2012 7:40 PM

Answers

  • Thanks for the prompt reply but there is already data in the table now so I need to add the column as follows:

    if (!dc.DatabaseExists()) {
        dc.CreateDatabase();
    }
    else {
      DatabaseSchemaUpdater dbUpdater = dc.CreateDatabaseSchemaUpdater();
      if (dbUpdater.DatabaseSchemaVersion < 1) {
        dbUpdater.AddColumn<MyTable>("MyColumn");
        dbUpdater.DatabaseSchemaVersion = 1;
        dbUpdater.Execute();
      }
    }

    I get the exception [System.Data.SqlServerCe.SqlCeException] {""} with HResult -2147467259. What am I doing wrong.
    Tuesday, January 17, 2012 8:10 PM

All replies

  • HI,

    Yes, you can do that:

    1. Add a new column col4, using SQL Server Management Studio, or other tools available you have
    2. Update your model or refresh,
    3. Check your EntityClasss of your table, you may need to add property col4 at the entity class. 
    4. Test your application

    Have fun

    Tuesday, January 17, 2012 8:04 PM
  • Thanks for the prompt reply but there is already data in the table now so I need to add the column as follows:

    if (!dc.DatabaseExists()) {
        dc.CreateDatabase();
    }
    else {
      DatabaseSchemaUpdater dbUpdater = dc.CreateDatabaseSchemaUpdater();
      if (dbUpdater.DatabaseSchemaVersion < 1) {
        dbUpdater.AddColumn<MyTable>("MyColumn");
        dbUpdater.DatabaseSchemaVersion = 1;
        dbUpdater.Execute();
      }
    }

    I get the exception [System.Data.SqlServerCe.SqlCeException] {""} with HResult -2147467259. What am I doing wrong.
    Tuesday, January 17, 2012 8:10 PM
  • Hi,

    I am so sorry, I have no any experience using SQL CE yet.

    Have fun

    Tuesday, January 17, 2012 8:19 PM