Adding a new field to existing table

Oct 9, 2012 at 2:00 PM

Hi,

I have database and TableA which contains some records

[Entity(KeyScheme.Identity)]
class TableA
{
    [Field(IsPrimaryKey = true)] public int id { get; set; }
    [Field] public int field_1 { get; set; }
}

but when I need to extend TableA and append a new field (field_2)

[Entity(KeyScheme.Identity)]
class TableA
{
    [Field(IsPrimaryKey = true)] public int id { get; set; }
    [Field] public int field_1 { get; set; }
    [Field] public int field_2 { get; set; }
}

now I get IndexOutOfRangeException due to the database does not have field_2 into TableA.
So, my question is there any simple way to add field_2 to exsisting table or I should create a new database with TableA::field_2 and move all records from the old database to the new one?

Coordinator
Oct 9, 2012 at 2:03 PM

After registering the EntityType call Store.EnsureCompatibility() and it will add the field to the database.

Oct 9, 2012 at 2:27 PM

So, it should be something like that:

 

            var Store = new SqlCeDataStore(databasePath)
            { ConnectionBehavior = ConnectionBehavior.Persistent, UseCommandCache = true };
            Store.DiscoverTypes(Assembly.GetExecutingAssembly());
            if (Store.StoreExists == false) Store.CreateStore();
            Store.EnsureCompatibility();

Right?

Coordinator
Oct 9, 2012 at 2:28 PM

Yes.

Oct 9, 2012 at 2:38 PM

It will return default value for field_2 for old records. Right?

Thanks. It works.