New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature suggestion: Migrations #4

Open
code1line opened this Issue Mar 27, 2015 · 37 comments

Comments

Projects
None yet
@code1line
Contributor

code1line commented Mar 27, 2015

What do you think about migrations-support?

@msallin

This comment has been minimized.

Show comment
Hide comment
@msallin

msallin Mar 28, 2015

Owner

Hi Michael

Of course migrations support would be great.
I have not much Experience with entity framework, but as fare as I know the EF Migrations mechanismus consists of two parts.

  1. MigrationSqlGenerator
    This class must be implemented. It will generate the SQL statements based on the commands. Which means this class needs to know how to tread a CreateTable() which is used in a migration.
  2. MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration> Class
    Normally you would use this initializer to use migrations. But as the code first stuff is done in a own initializer, this class could not be used. That means a) a own migrations class needs to be written or b) we find out what exactly the MigrateDatabaseToLatestVersionclass does and then just implement the necessary stuff (like create the History Table) so that the functionality of the MigrateDatabaseToLatestVersionclass could be reused.

It seems very complicated to me and may there is another way to do this.

Owner

msallin commented Mar 28, 2015

Hi Michael

Of course migrations support would be great.
I have not much Experience with entity framework, but as fare as I know the EF Migrations mechanismus consists of two parts.

  1. MigrationSqlGenerator
    This class must be implemented. It will generate the SQL statements based on the commands. Which means this class needs to know how to tread a CreateTable() which is used in a migration.
  2. MigrateDatabaseToLatestVersion<TContext, TMigrationsConfiguration> Class
    Normally you would use this initializer to use migrations. But as the code first stuff is done in a own initializer, this class could not be used. That means a) a own migrations class needs to be written or b) we find out what exactly the MigrateDatabaseToLatestVersionclass does and then just implement the necessary stuff (like create the History Table) so that the functionality of the MigrateDatabaseToLatestVersionclass could be reused.

It seems very complicated to me and may there is another way to do this.

@code1line

This comment has been minimized.

Show comment
Hide comment
@code1line

code1line Mar 28, 2015

Contributor

I agree - it would be great, but it does not seem to be an easy task. It may be planned for a future release...

Contributor

code1line commented Mar 28, 2015

I agree - it would be great, but it does not seem to be an easy task. It may be planned for a future release...

@msallin msallin added the enhancement label Mar 28, 2015

@msallin msallin added this to the v2 milestone Mar 28, 2015

@msallin msallin assigned msallin and unassigned msallin Mar 28, 2015

@msallin msallin changed the title from Feature-suggestion: Migrations to Feature suggestion: Migrations Apr 6, 2015

@GaryWolfe

This comment has been minimized.

Show comment
Hide comment
@GaryWolfe

GaryWolfe Jun 24, 2015

Migrations would be ideal, as that seems to be how EF is typically presented with Code First.

Is there some documentation on how to use EF with Code First but not with migrations? Everything I find seems to discuss migrations (which requires the above-mentioned sql generator class). When I try running "update-database", it asks for the sql generator. I'm sorry if this isn't the right forum for this question, but how do I use this project and SQLite without migrations?

I have this working as code-first (with migration) in SQL Server, but wanted to try SQLite, as the db is small and SQL Server is overkill.

GaryWolfe commented Jun 24, 2015

Migrations would be ideal, as that seems to be how EF is typically presented with Code First.

Is there some documentation on how to use EF with Code First but not with migrations? Everything I find seems to discuss migrations (which requires the above-mentioned sql generator class). When I try running "update-database", it asks for the sql generator. I'm sorry if this isn't the right forum for this question, but how do I use this project and SQLite without migrations?

I have this working as code-first (with migration) in SQL Server, but wanted to try SQLite, as the db is small and SQL Server is overkill.

@msallin

This comment has been minimized.

Show comment
Hide comment
@msallin

msallin Jun 24, 2015

Owner

Hi Gary

  1. You can use EF without migrations. Have a look at the Code in this Repo, there is a sample Application.
  2. You can not run "update-database" because there is no implementation for the SQL Generator.
  3. See 1. Just delete your migrations may be an option, because the Model (Code) should be up to date.

I once tried to dive into EF and find out how the migrations are implemented.
But I was not able to create a own sql generator.

In the (Issue 16)[https://github.com//issues/16] HareIM posted a link with an implementation for such an sql generator. May you can try to use this: https://drive.google.com/file/d/0B-NCqWnMQpBrQjkxMkxnVlRzb00/view?pli=1

Owner

msallin commented Jun 24, 2015

Hi Gary

  1. You can use EF without migrations. Have a look at the Code in this Repo, there is a sample Application.
  2. You can not run "update-database" because there is no implementation for the SQL Generator.
  3. See 1. Just delete your migrations may be an option, because the Model (Code) should be up to date.

I once tried to dive into EF and find out how the migrations are implemented.
But I was not able to create a own sql generator.

In the (Issue 16)[https://github.com//issues/16] HareIM posted a link with an implementation for such an sql generator. May you can try to use this: https://drive.google.com/file/d/0B-NCqWnMQpBrQjkxMkxnVlRzb00/view?pli=1

@GaryWolfe

This comment has been minimized.

Show comment
Hide comment
@GaryWolfe

GaryWolfe Jun 24, 2015

Thanks for the fast response, Marc! I think I can probably live without the migrations with my small project, but the Sql Generator sounds intriguing. I'll have a look at that and the sample code you mention as time permits.

Working on this last night, I kept getting a zero-byte SQLite database generated, so I must be doing something wrong; it did not re-build a freshly seeded db as I would have expected. Since I have been using SQL Server, it seems to hang on to that information somewhere. When I run, it creates a zero byte SQLite db, but then runs off of the SQL Server db! (I've commented-out the connection strings in the web.config for SQL Server, so not sure how it does that!) I like how EF automates things, but I guess it also hides what it's doing. (In other work, I use DB-first, and I think that's best for larger projects. I've had success working with that, so I've been tempted to go that route.) If I continue to be stuck, I'll just start over with a "fresh" project and copy files over, to ensure that it only uses SQLite.

Thanks for clarifying things.

GaryWolfe commented Jun 24, 2015

Thanks for the fast response, Marc! I think I can probably live without the migrations with my small project, but the Sql Generator sounds intriguing. I'll have a look at that and the sample code you mention as time permits.

Working on this last night, I kept getting a zero-byte SQLite database generated, so I must be doing something wrong; it did not re-build a freshly seeded db as I would have expected. Since I have been using SQL Server, it seems to hang on to that information somewhere. When I run, it creates a zero byte SQLite db, but then runs off of the SQL Server db! (I've commented-out the connection strings in the web.config for SQL Server, so not sure how it does that!) I like how EF automates things, but I guess it also hides what it's doing. (In other work, I use DB-first, and I think that's best for larger projects. I've had success working with that, so I've been tempted to go that route.) If I continue to be stuck, I'll just start over with a "fresh" project and copy files over, to ensure that it only uses SQLite.

Thanks for clarifying things.

@msallin msallin modified the milestones: TBD, v1.1.0.0 Jan 15, 2016

@HarelM

This comment has been minimized.

Show comment
Hide comment
@HarelM

HarelM commented Jan 18, 2016

+1

@tg73

This comment has been minimized.

Show comment
Hide comment
@tg73

tg73 Feb 12, 2016

I've got a working prototype for migrations. It was originally based on the prototype put together by Julio C. Borges in 2013, although significant further work was required. Several migration operations are currently not implemented, notably those ALTER TABLE operations which are not natively supported by SQLite (see https://www.sqlite.org/lang_altertable.html).

The code is not as clean or well-formatted as I'd like for issuing a pull request, so for the time being you can get it here:

https://github.com/zanyants/SQLiteCodeFirst/tree/feature/migrations

To use:

To your migrations Configuration class ctor, add:

SetSqlGenerator( "System.Data.SQLite", new SQLite.CodeFirst.SqliteMigrationSqlGenerator() );

And in your context's OnModelCreating add:

Database.SetInitializer( new System.Data.Entity.MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>( true ) );

tg73 commented Feb 12, 2016

I've got a working prototype for migrations. It was originally based on the prototype put together by Julio C. Borges in 2013, although significant further work was required. Several migration operations are currently not implemented, notably those ALTER TABLE operations which are not natively supported by SQLite (see https://www.sqlite.org/lang_altertable.html).

The code is not as clean or well-formatted as I'd like for issuing a pull request, so for the time being you can get it here:

https://github.com/zanyants/SQLiteCodeFirst/tree/feature/migrations

To use:

To your migrations Configuration class ctor, add:

SetSqlGenerator( "System.Data.SQLite", new SQLite.CodeFirst.SqliteMigrationSqlGenerator() );

And in your context's OnModelCreating add:

Database.SetInitializer( new System.Data.Entity.MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>( true ) );

@msallin

This comment has been minimized.

Show comment
Hide comment
@msallin

msallin Feb 17, 2016

Owner

Hello Tom

Thanks for your work!
Currently I'm very busy and have not the chance to look through it in detail.
The formatting should not be the problem, R# will to the hard work ;)
It needs some refactoring but I'm glad to work together with you on this code.

I created a branch https://github.com/msallin/SQLiteCodeFirst/tree/Issue-4 you can create a PR to push your changes in this branch and then we can work on it.

Greetings Marc

Owner

msallin commented Feb 17, 2016

Hello Tom

Thanks for your work!
Currently I'm very busy and have not the chance to look through it in detail.
The formatting should not be the problem, R# will to the hard work ;)
It needs some refactoring but I'm glad to work together with you on this code.

I created a branch https://github.com/msallin/SQLiteCodeFirst/tree/Issue-4 you can create a PR to push your changes in this branch and then we can work on it.

Greetings Marc

@tg73

This comment has been minimized.

Show comment
Hide comment
@tg73

tg73 Feb 19, 2016

Hi Marc,

I'm also rather busy - I should have time to do as you ask early next week.

Tom

tg73 commented Feb 19, 2016

Hi Marc,

I'm also rather busy - I should have time to do as you ask early next week.

Tom

@msallin

This comment has been minimized.

Show comment
Hide comment
@msallin

msallin Mar 4, 2016

Owner

@CADbloke thanks for the hint!
@tg73 What do you think, could we benefit from that?

Owner

msallin commented Mar 4, 2016

@CADbloke thanks for the hint!
@tg73 What do you think, could we benefit from that?

@CADbloke

This comment has been minimized.

Show comment
Hide comment
@CADbloke

CADbloke May 2, 2016

More (actually, less progress but more discussion) from the EF team not getting far with SQLite migrations. This issue has links to a lot of other related issues in the EF7 project. It seems to be the main issue they are tracking on the matter.

aspnet/EntityFrameworkCore#329

CADbloke commented May 2, 2016

More (actually, less progress but more discussion) from the EF team not getting far with SQLite migrations. This issue has links to a lot of other related issues in the EF7 project. It seems to be the main issue they are tracking on the matter.

aspnet/EntityFrameworkCore#329

@HarelM

This comment has been minimized.

Show comment
Hide comment
@HarelM

HarelM Jun 11, 2016

Any updates on this? I think it's almost the only open issue left... :-)

HarelM commented Jun 11, 2016

Any updates on this? I think it's almost the only open issue left... :-)

@HarelM

This comment has been minimized.

Show comment
Hide comment
@HarelM

HarelM Feb 24, 2017

I was wondering how the fact that SQLite is supported using .Net Core and EF.Core would affect this issue. see here: https://docs.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite

HarelM commented Feb 24, 2017

I was wondering how the fact that SQLite is supported using .Net Core and EF.Core would affect this issue. see here: https://docs.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite

@msallin

This comment has been minimized.

Show comment
Hide comment
@msallin

msallin Feb 24, 2017

Owner

I while ago I looked through the code (as its OSS :)) but I noticed thats not just "take and use".
There is still the started work from @tg73 (https://github.com/zanyants/SQLiteCodeFirst/tree/feature/migrations).

I think this is a lot of work to do and I'm currently not planning to work on it by my self.
I'm looking forward to invest some more time in spare time projects after I graduated as BSc. Still one year to go and I need my free time for the projects there.

Owner

msallin commented Feb 24, 2017

I while ago I looked through the code (as its OSS :)) but I noticed thats not just "take and use".
There is still the started work from @tg73 (https://github.com/zanyants/SQLiteCodeFirst/tree/feature/migrations).

I think this is a lot of work to do and I'm currently not planning to work on it by my self.
I'm looking forward to invest some more time in spare time projects after I graduated as BSc. Still one year to go and I need my free time for the projects there.

@digocesar

This comment has been minimized.

Show comment
Hide comment
@digocesar

digocesar Mar 1, 2017

I'm using @tg73 work and is working fine, with some adjusts on index names.

digocesar commented Mar 1, 2017

I'm using @tg73 work and is working fine, with some adjusts on index names.

@tg73

This comment has been minimized.

Show comment
Hide comment
@tg73

tg73 Mar 1, 2017

tg73 commented Mar 1, 2017

@msallin

This comment has been minimized.

Show comment
Hide comment
@msallin
Owner

msallin commented Mar 18, 2017

Maybe worth to have a look:
https://github.com/ericschultz/SQLiteParser

@theShiva

This comment has been minimized.

Show comment
Hide comment
@theShiva

theShiva Mar 26, 2017

Hi @msallin

Excellent work on this Library! Love it!! I just tried to do an Add-Migration ... and realized that Migrations is not supported by this Library.

What's the point of using this for Code-First, if migrations is not supported. In any practical real-life project, the database is not just created 1 time and forgotten. It will undergo many changes during it's lifetime.

Any chance anyone else will be able to take this on?

Thank you!
-Shiva

theShiva commented Mar 26, 2017

Hi @msallin

Excellent work on this Library! Love it!! I just tried to do an Add-Migration ... and realized that Migrations is not supported by this Library.

What's the point of using this for Code-First, if migrations is not supported. In any practical real-life project, the database is not just created 1 time and forgotten. It will undergo many changes during it's lifetime.

Any chance anyone else will be able to take this on?

Thank you!
-Shiva

@CADbloke

This comment has been minimized.

Show comment
Hide comment
@CADbloke

CADbloke Apr 17, 2017

The ASP.NET team are still onto this: aspnet/EntityFrameworkCore#7969 as well as the previously mentioned aspnet/EntityFrameworkCore#329

I have been thoroughly distracted by client work, those pesky rascals, so have been unable to dive into this. Yet. :(

CADbloke commented Apr 17, 2017

The ASP.NET team are still onto this: aspnet/EntityFrameworkCore#7969 as well as the previously mentioned aspnet/EntityFrameworkCore#329

I have been thoroughly distracted by client work, those pesky rascals, so have been unable to dive into this. Yet. :(

@msallin msallin referenced this issue May 31, 2017

Closed

lost data #88

@CADbloke

This comment has been minimized.

Show comment
Hide comment
@CADbloke

CADbloke Jun 6, 2017

also found: https://github.com/bubibubi/sqliteef6migrations
...edit... https://www.codeproject.com/Articles/220018/SQLite-Compare-Utility may be handy. Perhaps a way is to create an new empty database if SqliteCreateDatabaseIfNotExists fails and use the diff migrate the existing database to the schema of the new empty database.

CADbloke commented Jun 6, 2017

also found: https://github.com/bubibubi/sqliteef6migrations
...edit... https://www.codeproject.com/Articles/220018/SQLite-Compare-Utility may be handy. Perhaps a way is to create an new empty database if SqliteCreateDatabaseIfNotExists fails and use the diff migrate the existing database to the schema of the new empty database.

@AndreaMarchetto

This comment has been minimized.

Show comment
Hide comment
@AndreaMarchetto

AndreaMarchetto Aug 24, 2017

I wrote "SqliteDropCreateAndMigrateDatabaseWhenModelChanges" class derived from "SqliteDropCreateDatabaseWhenModelChanges" (the class i attached).

With this class you can write something like this:

public class TLocalDatabase : DbContext
{
	
	...
	
	protected override void OnModelCreating(DbModelBuilder modelBuilder)
	{
		TLocalDatabaseInitializer strategy = new TLocalDatabaseInitializer(modelBuilder);
		Database.SetInitializer<TLocalDatabase>(strategy);
	}
	
	....
	
}


 public class TLocalDatabaseInitializer: SqliteDropCreateAndMigrateDatabaseWhenModelChanges<TLocalDatabase>
    {
        public TLocalDatabaseInitializer(DbModelBuilder modelBuilder) : base(modelBuilder) {
            TablesMigrationValidation += TLocalDatabaseInitializer_TablesMigrationValidation;
            FieldsMigrationValidation += TLocalDatabaseInitializer_FieldsMigrationValidation;
            BeforeTableMigration += TLocalDatabaseInitializer_BeforeTableMigration;
        }

        private void TLocalDatabaseInitializer_TablesMigrationValidation(object sender, TablesMigrationValidationEventArgs e)
        {
            // Here you can verify which tables are added and/or removed since the previous version, see: "e.added_tables" and "e.dropped_tables"

            // If you want to abort migration simply:
            e.Cancel = true;
        }

        private void TLocalDatabaseInitializer_FieldsMigrationValidation(object sender, FieldsMigrationValidationEventArgs e)
        {
            // Here you can verify which fields in the specified table are added and/or removed since the previous version, see: "e.table_name", "e.added_fields" and "e.dropped_fields"

            // If you want to abort migration simply:
            e.Cancel = true;
        }

        private void TLocalDatabaseInitializer_BeforeTableMigration(object sender, BeforeTableMigrationEventArgs e)
        {
            // Here you can personalize the migration of datas for each tables.
            // For example:
            // in the model I want to check some problems that should affect the table "DemoTable":
            if (e.tbl_name == "DemoTable")
            {
                // if a text field named "NewTextField" has been added (suppose it is "NOT NULL") for allowing the migration I need to initialize it to the fixed value "unknown":
                if (!e.from_list.Contains("[NewTextField]") && e.to_list.Contains("[NewTextField]"))
                {
                    e.from_list.Add("'unknown'");
                    e.to_list.Add("[NewTextField]");
                }
                // if an integer field named "NewIntField" has been added (suppose it is "NOT NULL") for allowing the migration I need to initialize it copying the existing field "ExistingIntField" incremented by 1:
                if (!e.from_list.Contains("[NewIntField]") && e.to_list.Contains("[NewIntField]"))
                {
                    e.from_list.Add("[ExistingIntField]+1");
                    e.to_list.Add("[NewIntField]");
                }
            }
        }

        protected override void SeedAfterCreation(TLocalDatabase context)
        {
            base.SeedAfterCreation(context);

            // Called if the database is created for the first time (there is no previous version from which import datas).
            // Here you can populate the database with the initial datas
        }

        protected override void SeedAfterMigration(TLocalDatabase context)
        {
            base.SeedAfterMigration(context);

            // Called if the database was recreated because the model has changed (datas from previous version are yet imported).
            // Here you can modify datas after the migration.
        }
    }

PS:
I wrote it very quickly so sorry, it is uncommented and not well formed!
The best solution is to fork SQLiteCodeFirst and add this class in it (some optimization are possible in this case!)
SqliteDropCreateAndMigrateDatabaseWhenModelChanges.zip

AndreaMarchetto commented Aug 24, 2017

I wrote "SqliteDropCreateAndMigrateDatabaseWhenModelChanges" class derived from "SqliteDropCreateDatabaseWhenModelChanges" (the class i attached).

With this class you can write something like this:

public class TLocalDatabase : DbContext
{
	
	...
	
	protected override void OnModelCreating(DbModelBuilder modelBuilder)
	{
		TLocalDatabaseInitializer strategy = new TLocalDatabaseInitializer(modelBuilder);
		Database.SetInitializer<TLocalDatabase>(strategy);
	}
	
	....
	
}


 public class TLocalDatabaseInitializer: SqliteDropCreateAndMigrateDatabaseWhenModelChanges<TLocalDatabase>
    {
        public TLocalDatabaseInitializer(DbModelBuilder modelBuilder) : base(modelBuilder) {
            TablesMigrationValidation += TLocalDatabaseInitializer_TablesMigrationValidation;
            FieldsMigrationValidation += TLocalDatabaseInitializer_FieldsMigrationValidation;
            BeforeTableMigration += TLocalDatabaseInitializer_BeforeTableMigration;
        }

        private void TLocalDatabaseInitializer_TablesMigrationValidation(object sender, TablesMigrationValidationEventArgs e)
        {
            // Here you can verify which tables are added and/or removed since the previous version, see: "e.added_tables" and "e.dropped_tables"

            // If you want to abort migration simply:
            e.Cancel = true;
        }

        private void TLocalDatabaseInitializer_FieldsMigrationValidation(object sender, FieldsMigrationValidationEventArgs e)
        {
            // Here you can verify which fields in the specified table are added and/or removed since the previous version, see: "e.table_name", "e.added_fields" and "e.dropped_fields"

            // If you want to abort migration simply:
            e.Cancel = true;
        }

        private void TLocalDatabaseInitializer_BeforeTableMigration(object sender, BeforeTableMigrationEventArgs e)
        {
            // Here you can personalize the migration of datas for each tables.
            // For example:
            // in the model I want to check some problems that should affect the table "DemoTable":
            if (e.tbl_name == "DemoTable")
            {
                // if a text field named "NewTextField" has been added (suppose it is "NOT NULL") for allowing the migration I need to initialize it to the fixed value "unknown":
                if (!e.from_list.Contains("[NewTextField]") && e.to_list.Contains("[NewTextField]"))
                {
                    e.from_list.Add("'unknown'");
                    e.to_list.Add("[NewTextField]");
                }
                // if an integer field named "NewIntField" has been added (suppose it is "NOT NULL") for allowing the migration I need to initialize it copying the existing field "ExistingIntField" incremented by 1:
                if (!e.from_list.Contains("[NewIntField]") && e.to_list.Contains("[NewIntField]"))
                {
                    e.from_list.Add("[ExistingIntField]+1");
                    e.to_list.Add("[NewIntField]");
                }
            }
        }

        protected override void SeedAfterCreation(TLocalDatabase context)
        {
            base.SeedAfterCreation(context);

            // Called if the database is created for the first time (there is no previous version from which import datas).
            // Here you can populate the database with the initial datas
        }

        protected override void SeedAfterMigration(TLocalDatabase context)
        {
            base.SeedAfterMigration(context);

            // Called if the database was recreated because the model has changed (datas from previous version are yet imported).
            // Here you can modify datas after the migration.
        }
    }

PS:
I wrote it very quickly so sorry, it is uncommented and not well formed!
The best solution is to fork SQLiteCodeFirst and add this class in it (some optimization are possible in this case!)
SqliteDropCreateAndMigrateDatabaseWhenModelChanges.zip

@CADbloke

This comment has been minimized.

Show comment
Hide comment
@CADbloke

CADbloke Sep 13, 2017

I haven't checked this out thoroughly yet but it looks interesting ... https://www.codeproject.com/Articles/32977/Upgrade-framework-for-SQLite-databases

CADbloke commented Sep 13, 2017

I haven't checked this out thoroughly yet but it looks interesting ... https://www.codeproject.com/Articles/32977/Upgrade-framework-for-SQLite-databases

@Rgl88

This comment has been minimized.

Show comment
Hide comment
@Rgl88

Rgl88 Oct 18, 2017

Hello @digocesar

I use following codes but methods never called in SqliteMigrationSqlGenerator !

To use:

To your migrations Configuration class ctor, add:

SetSqlGenerator( "System.Data.SQLite", new SQLite.CodeFirst.SqliteMigrationSqlGenerator() );

And in your context's OnModelCreating add:

Database.SetInitializer( new System.Data.Entity.MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>( true ) );

But i apply your changes to code and can not solve double quotation problem!

...
.Index(t => t.LastModifierUserId, name: ""IX_Appliance_LastModifierUserId"")
...

After replace "" with "

PM> add-migration "Ref_Db_Migration_v1.1"
Unable to generate an explicit migration because the following explicit migrations are pending: [201710152025217_Ref_Db_Migration_v1.0]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.Data.Entity.Core.ProviderIncompatibleException: CreateDatabase is not supported by the provider.
   at System.Data.Entity.Core.Common.DbProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   at System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
   at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
CreateDatabase is not supported by the provider.

Rgl88 commented Oct 18, 2017

Hello @digocesar

I use following codes but methods never called in SqliteMigrationSqlGenerator !

To use:

To your migrations Configuration class ctor, add:

SetSqlGenerator( "System.Data.SQLite", new SQLite.CodeFirst.SqliteMigrationSqlGenerator() );

And in your context's OnModelCreating add:

Database.SetInitializer( new System.Data.Entity.MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>( true ) );

But i apply your changes to code and can not solve double quotation problem!

...
.Index(t => t.LastModifierUserId, name: ""IX_Appliance_LastModifierUserId"")
...

After replace "" with "

PM> add-migration "Ref_Db_Migration_v1.1"
Unable to generate an explicit migration because the following explicit migrations are pending: [201710152025217_Ref_Db_Migration_v1.0]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.Data.Entity.Core.ProviderIncompatibleException: CreateDatabase is not supported by the provider.
   at System.Data.Entity.Core.Common.DbProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   at System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
   at System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
   at System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
CreateDatabase is not supported by the provider.
@digocesar

This comment has been minimized.

Show comment
Hide comment
@digocesar

digocesar Oct 18, 2017

Hi @Rgl88.
I suggest to try:

Database.SetInitializer( new System.Data.Entity.MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>( true ) );
MyContext.Database.Initialize(true);

My problem with Index was when I need to drop it. Not creating. And I'm not using double quotation in it!

digocesar commented Oct 18, 2017

Hi @Rgl88.
I suggest to try:

Database.SetInitializer( new System.Data.Entity.MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>( true ) );
MyContext.Database.Initialize(true);

My problem with Index was when I need to drop it. Not creating. And I'm not using double quotation in it!

@Rgl88

This comment has been minimized.

Show comment
Hide comment
@Rgl88

Rgl88 Oct 19, 2017

Hello @digocesar
Double quotation generated via ef in migration file (Up method), when i add migration.
I try your code but it not work:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
            Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<DataContext, Migrations.Configuration>(true));
            Database.Initialize(true);

            //var sqliteDbInitializer = new SqliteDatabaseInitializer(modelBuilder);
            //Database.SetInitializer(sqliteDbInitializer);
}

Exception:

PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.InvalidOperationException: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.MarkDatabaseInitialized()
   at System.Data.Entity.Database.Initialize(Boolean force)
   at RefrigeratorTester.Data.DataContext.OnModelCreating(DbModelBuilder modelBuilder) in G:\Projects\...\Data\DataContext.cs:line 71
   at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.

Database property in MyContext.Database is not static!
When create new instance of DataContext, again throw exception (Object reference not set to an instance of an object.)
new DataContext().Database.Initialize(true);

Rgl88 commented Oct 19, 2017

Hello @digocesar
Double quotation generated via ef in migration file (Up method), when i add migration.
I try your code but it not work:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
            Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<DataContext, Migrations.Configuration>(true));
            Database.Initialize(true);

            //var sqliteDbInitializer = new SqliteDatabaseInitializer(modelBuilder);
            //Database.SetInitializer(sqliteDbInitializer);
}

Exception:

PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.InvalidOperationException: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.MarkDatabaseInitialized()
   at System.Data.Entity.Database.Initialize(Boolean force)
   at RefrigeratorTester.Data.DataContext.OnModelCreating(DbModelBuilder modelBuilder) in G:\Projects\...\Data\DataContext.cs:line 71
   at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
   at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
   at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
   at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.

Database property in MyContext.Database is not static!
When create new instance of DataContext, again throw exception (Object reference not set to an instance of an object.)
new DataContext().Database.Initialize(true);

@tuanth89

This comment has been minimized.

Show comment
Hide comment
@tuanth89

tuanth89 Nov 8, 2017

Hi @Rgl88 ,
I have same problem, which way to resolve that ?
Thanks

tuanth89 commented Nov 8, 2017

Hi @Rgl88 ,
I have same problem, which way to resolve that ?
Thanks

@tuanth89

This comment has been minimized.

Show comment
Hide comment
@tuanth89

tuanth89 Nov 8, 2017

Hi @AndreaMarchetto ,
I tried and have a problem about loss data:
I have a Db, include table Test (Id, Name, Desc) and records

  • Change model Test (ex : add field Status), run first app migration success (include field Status and table have record),
  • Close app and run again : include field Status but old data loss (table empty)
    I think problem at class SqliteDropCreateAndMigrateDatabaseWhenModelChanges ==> InitializeDatabase (method) ==> base.InitializeDatabase(context), after migration success and at second run, it still drop and create again.
    Somehow to resolve this??
    Thanks

UPDATE
I resolved by comment line code in SqliteDropCreateAndMigrateDatabaseWhenModelChanges.cs

string tbl_name = string.Format("History_{0}", History.TableNamePostfix);
List fld_list = new List() { "Hash", "Context", "CreateDate" };
ImportTable(from_hash, to_hash, cmd, tbl_name, fld_list);

=> keep data and add new field

tuanth89 commented Nov 8, 2017

Hi @AndreaMarchetto ,
I tried and have a problem about loss data:
I have a Db, include table Test (Id, Name, Desc) and records

  • Change model Test (ex : add field Status), run first app migration success (include field Status and table have record),
  • Close app and run again : include field Status but old data loss (table empty)
    I think problem at class SqliteDropCreateAndMigrateDatabaseWhenModelChanges ==> InitializeDatabase (method) ==> base.InitializeDatabase(context), after migration success and at second run, it still drop and create again.
    Somehow to resolve this??
    Thanks

UPDATE
I resolved by comment line code in SqliteDropCreateAndMigrateDatabaseWhenModelChanges.cs

string tbl_name = string.Format("History_{0}", History.TableNamePostfix);
List fld_list = new List() { "Hash", "Context", "CreateDate" };
ImportTable(from_hash, to_hash, cmd, tbl_name, fld_list);

=> keep data and add new field

@Rgl88

This comment has been minimized.

Show comment
Hide comment
@Rgl88

Rgl88 Nov 8, 2017

Hello @tuanth89
Unfortunately I could not find a solution!
It seems like you have been able to solve the problem 👍

Rgl88 commented Nov 8, 2017

Hello @tuanth89
Unfortunately I could not find a solution!
It seems like you have been able to solve the problem 👍

@AndreaMarchetto

This comment has been minimized.

Show comment
Hide comment
@AndreaMarchetto

AndreaMarchetto Nov 13, 2017

Hello @tuanth89
You are right...
Sorry, I founded this bug some time ago but I forgot to update my post!

AndreaMarchetto commented Nov 13, 2017

Hello @tuanth89
You are right...
Sorry, I founded this bug some time ago but I forgot to update my post!

@CADbloke

This comment has been minimized.

Show comment
Hide comment
@CADbloke

CADbloke Dec 4, 2017

Just leaving this here so I don't lose it - looks useful ...
https://github.com/praeclarum/sqlite-net/wiki/AutomaticMigrations
https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs#L489 <== code that maps an existing table to a new table during CreateTable

CADbloke commented Dec 4, 2017

Just leaving this here so I don't lose it - looks useful ...
https://github.com/praeclarum/sqlite-net/wiki/AutomaticMigrations
https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs#L489 <== code that maps an existing table to a new table during CreateTable

@JobaDiniz

This comment has been minimized.

Show comment
Hide comment
@JobaDiniz

JobaDiniz May 2, 2018

SetSqlGenerator( "System.Data.SQLite", new SQLite.CodeFirst.SqliteMigrationSqlGenerator() );
This line doesn't work anymore in EF 6.2.0, the Generate method on the CodeFirst.SqliteMigrationSqlGenerator doesn't get called.

Anyone have this issue?

JobaDiniz commented May 2, 2018

SetSqlGenerator( "System.Data.SQLite", new SQLite.CodeFirst.SqliteMigrationSqlGenerator() );
This line doesn't work anymore in EF 6.2.0, the Generate method on the CodeFirst.SqliteMigrationSqlGenerator doesn't get called.

Anyone have this issue?

@digocesar

This comment has been minimized.

Show comment
Hide comment
@digocesar

digocesar May 10, 2018

Hi @JobaDiniz .
Did you changed the Database Initializer?
See if this information can help you.

digocesar commented May 10, 2018

Hi @JobaDiniz .
Did you changed the Database Initializer?
See if this information can help you.

@JobaDiniz

This comment has been minimized.

Show comment
Hide comment
@JobaDiniz

JobaDiniz May 11, 2018

JobaDiniz commented May 11, 2018

@msallin

This comment has been minimized.

Show comment
Hide comment
Owner

msallin commented Aug 1, 2018

@JobaDiniz

This comment has been minimized.

Show comment
Hide comment
@JobaDiniz

JobaDiniz Aug 1, 2018

https://github.com/msallin/SQLiteCodeFirst/tree/%234

@msallin is this link correct? it goes to the home page of this repository...

JobaDiniz commented Aug 1, 2018

https://github.com/msallin/SQLiteCodeFirst/tree/%234

@msallin is this link correct? it goes to the home page of this repository...

@msallin

This comment has been minimized.

Show comment
Hide comment
@msallin

msallin Aug 2, 2018

Owner

Points to the branch #4, into which I merged your PR. I'll need some time to look through the code and I'll merge it into the master as soon as EF 6.3 with the fix was released and I integrated it into SQLite.CodeFirst.

Owner

msallin commented Aug 2, 2018

Points to the branch #4, into which I merged your PR. I'll need some time to look through the code and I'll merge it into the master as soon as EF 6.3 with the fix was released and I integrated it into SQLite.CodeFirst.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment