msallin / SQLiteCodeFirst

Creates a SQLite Database based on a EdmModel by using Entity Framework CodeFirst.
Apache License 2.0
608 stars 123 forks source link

migration issue #126

Closed ghost1372 closed 6 years ago

ghost1372 commented 6 years ago

hi @digocesar i created a table after that i added a new field until here everythings work fine but after that i removed that filed and when i run my app and select some data from db I expect the database to be reCreate but i get below error: result in data loss. Set AutomaticMigrationDataLossAllowed to 'true' on your DbMigrationsConfiguration to allow application of automatic migrations even if they might cause data loss. Alternately, use Update-Database with the '-Force' option, or scaffold an explicit migration.' my query: var query = db.Logins.Where(x => x.User == txtUser.Text && x.Pass == Hash).Any(); configuration:

 public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            SetSqlGenerator("System.Data.SQLite", new SqliteMigrationSqlGenerator());
        }

        protected override void Seed(Azmongir.MyDbContext context)
        {
            if (context.Set<Login>().Count() != 0)
            {
                return;
            }
            context.Set<Login>().Add(new Login
            {
                User = "test",
                Pass = "15-E2-B0-D3-C3-38-91-EB-B0-F1-EF-60-9E-C4-19-42-0C-20-E3-20-CE-94-C6-5F-BC-8C-33-12-44-8E-B2-25"
            });
            context.SaveChanges();
        }

and mydbContext:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer(new SqliteMigrateDatabaseToLatestVersion<MyDbContext, Migrations.Configuration>(modelBuilder, true));
        }
digocesar commented 6 years ago

Hi @ghost1372, Did you create a Migration Update to apply you changes to database structure? You need to use "Add-Migration" command into Console Package Manager to create a database change. I had working into an example before reintegrate my project to @msallin project. But I han't time to finish yet.

ghost1372 commented 6 years ago

this is my project untitled after removing field i send this command update-database but i get error: Cannot access a disposed object. Object name: 'SQLiteConnection'.

ghost1372 commented 6 years ago

@digocesar i downgraded entityframwork from 6.2 to 6.1.3 now when i send update-database i get following error: PM> update-database Specify the '-Verbose' flag to view the SQL statements being applied to the target database. No pending explicit migrations. Applying automatic migration: 201805281149059_AutomaticMigration. System.NotImplementedException: DropColumn is non-trivial and has not been implemented. See http://sqlite.org/lang_altertable.html

digocesar commented 6 years ago

EF 6.2 introduced an error with Migrations (#398) and it was solved to EF 1.3. So you are right to downgrade to EF 6.1.3 as workaround.

But you project is missing Migrations files with database changes info. Please read this post and get more information about Migrations on the web.

ghost1372 commented 6 years ago

@digocesar ok tnx, and if you have time, look at this https://qiita.com/minoru-nagasawa/items/961f6eae809a379c1b52

msallin commented 6 years ago

https://github.com/msallin/SQLiteCodeFirst/issues/125