1

I'm using WAF ( WPF Application Framework ) in here: https://waf.codeplex.com. And I open the BookLibrary project in it sample. I have an model named Author and it related class.

And this is it DbContext:..

internal class BookLibraryContext : DbContext
    {
        public BookLibraryContext(DbConnection dbConnection)
            : base(dbConnection, false)
        {
            Database.SetInitializer<BookLibraryContext>(null);
        }

        public BookLibraryContext()
            : base(@"Data Source=|DataDirectory|\Resources\BookLibrary2.sdf")
        {

        }

        public bool HasChanges
        {
            get
            {
                ChangeTracker.DetectChanges();

                // It is necessary to ask the ObjectContext if changes could be detected because the
                // DbContext does not provide the information when a navigation property has changed.
                return ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Any()
                    || ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified).Any()
                    || ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted).Any();
            }
        }

        private ObjectContext ObjectContext { get { return ((IObjectContextAdapter)this).ObjectContext; } }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new PersonMapping());
            modelBuilder.Configurations.Add(new BookMapping());
            modelBuilder.Configurations.Add(new AuthorMapping());
        }
    }

When i run the project. An exception occured:

{"The specified table does not exist. [ Author ]"}

How do i add new table named Author ? I know using Entity Framework migration or edit the database structor with tools.

But I see the method named HasChange(). It may do something to reflect my database. But I don't know to make it work. Please help me

dovid
  • 6,354
  • 3
  • 33
  • 73
user2877989
  • 587
  • 1
  • 6
  • 19
  • http://stackoverflow.com/questions/2375118/how-to-open-sdf-files you may need to open the database BookLibrary2.sdf to add the table. – ray Mar 31 '14 at 06:30

1 Answers1

0

You are using Database.SetInitializer<BookLibraryContext>(null); which causes Entity Framework to do no initialization when starting up, meaning your Data needs to already match your classes.

You can use the following initializers instead of null:

  1. CreateDatabaseIfNotExists. This will create a new database if none exists. However, changing the model will not recreate the database, and the program will error.
  2. DropCreateDatabaseIfModelChanges This will create a new database if the model changes, or leave the existing database if the model has stayed the same.
  3. DropCreateDatabaseAlways This will create a new database every time the program is run.
  4. MigrateDatabaseToLatestVersion This will process Entity Framework Migrations, to preserve the data in the database and add or remove tables as necessary (also called Data Motion).

Migrations are the hardest to set up, but the easiest to maintain, in the long run. However, for testing, any option would be fine.

An example of using one of the initializers would be:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BookLibraryContext>());

Claies
  • 22,124
  • 4
  • 53
  • 77