bubibubi / sqliteef6migrations

System.Data.SQLite.EntityFramework.Migrations - Migrations for SQLite Entity Framework provider
Microsoft Public License
40 stars 20 forks source link

Question #7

Closed alex6dj closed 6 years ago

alex6dj commented 6 years ago

I'm trying migrations with your library. It's working OK with your example:

internal sealed class ContextMigrationConfiguration : DbMigrationsConfiguration<Context>
    {
        public ContextMigrationConfiguration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
            SetSqlGenerator("System.Data.SQLite", new SQLiteMigrationSqlGenerator());
        }
    }

But when I try to do this it fails:

public sealed class ContextMigrationConfiguration : DbMigrationsConfiguration<UnitOfWork>
    {
        private readonly bool _pendingMigrations;

        public ContextMigrationConfiguration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
            SetSqlGenerator("System.Data.SQLite", new SQLiteMigrationSqlGenerator());

            var migrator = new DbMigrator(this);
            _pendingMigrations = migrator.GetPendingMigrations().Any();

            if (_pendingMigrations)
            {
                migrator.Update();
                Seed(new UnitOfWork());
            }
        }

        protected sealed override void Seed(UnitOfWork context) 
        {
            // Initialization
        }
   }

_pendingMigrations = migrator.GetPendingMigrations().Any(); Fails with System.ObjectDisposedException on .GetPendingMigrations()

  | ObjectName | "SQLiteConnection"   | Source | "System.Data.SQLite"

  | StackTrace | " en System.Data.SQLite.SQLiteConnection.CheckDisposed()\r\n en System.Data.SQLite.SQLiteConnection.get_State()\r\n en System.Data.Entity.Internal.RepositoryBase.CreateConnection()\r\n en System.Data.Entity.Migrations.History.HistoryRepository.QueryExists(String contextKey)\r\n en System.Data.Entity.Migrations.History.HistoryRepository.Exists(String contextKey)\r\n en System.Data.Entity.Migrations.History.HistoryRepository.GetPendingMigrations(IEnumerable`1 localMigrations)\r\n en System.Data.Entity.Migrations.DbMigrator.GetPendingMigrations()\r\n en QApps.DataLayer.Context.ContextMigrationConfiguration..ctor() en D:\Projects\...\Context\UnitOfWork.cs:línea 91"

I'm trying to init the db with some test data, but if I use Seed directly it set the data every time the app is launched, so I'm trying to Seed only after an update.

I will try the same question on StackOverflow just in case, any help will be apreciated.

alex6dj commented 6 years ago

One thing I don't have migrations enabled yet, I just use it to create the db and populate it with some test data

bubibubi commented 6 years ago

GetPendingMigrations execute a query on __MigrationHistory and requires an opened connection. Does it work with other providers? Try to ask on StackOverflow for more info.

alex6dj commented 6 years ago

OK, maybe this is the problem. I´m actually not using migrations (yet) then __MigrationHistory don´t exist in my db. I will do some test and let you know. Is the first time I use this, before I use sql ce and it works out of the box, but is too slow, I prefer sqlite, but there are a lot of tweaks to make it work without troubles. And I soo new to code first...

Thanks, I will make some test and if this is the problem I will find another way to seed the db with new data.

alex6dj commented 6 years ago

Ok this was the problem. I´m still developing the data layer, when I start using migrations I will try. Thanks for your anwer and your time. By the way I now using your library to generate al tables in db from code first and it works as expected. Good job