Code-First automatic migrations problems with dotConnect 5.1.36 and EF 6.0.1 Code-First

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Code-First automatic migrations problems with dotConnect 5.1.36 and EF 6.0.1 Code-First

Post by bairog » Wed 11 Dec 2013 12:20

Hello.
I'm using dotConnect for SQLite 5.1.36 with Entity Framework 6.0.1 Code-First
I'm trying to create automatic migrations with configuration

Code: Select all

internal sealed class SysTypesDbContextMigrationConfiguration : DbMigrationsConfiguration<MyContext>
        {
            public SysTypesDbContextMigrationConfiguration()
            {
                AutomaticMigrationsEnabled = true;
                AutomaticMigrationDataLossAllowed = false;

                SetSqlGenerator(SQLiteConnectionInfo.InvariantName, new SQLiteEntityMigrationSqlGenerator());
            }
        }
and initializer

Code: Select all

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, SysTypesDbContextMigrationConfiguration>()); 
To encounter first problem I:
1)run my sample code - database is created
2)change entities (remove comment in my sample code to add field)
3)enable migrations (remove comment in my sample code near Database.SetInitializer)
then exception is raised
The target context 'EF6Test.MyContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory.
So what should I do?

Second problem(bug?) is that if I have only configuration in my code (without Database.SetInitializer) and have no database - exception is thrown
Migrations is enabled for context 'MyContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.
That is situation appears without Database.SetInitializer, with CreateDatabaseIfNotExist, DropCreateDatabaseAlways and DropCreateIfModelChanged.

I supposed migrations mechanism to be switched on with

Code: Select all

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, SysTypesDbContextMigrationConfiguration>()); 
, isn't it?
Why SysTypesDbContextMigrationConfiguration is taken into consideration without MigrateDatabaseToLatestVersion?

BTW
Maybe you can reproduce this simply by running my code (without existing database), but I encountered it first time after disabling

Code: Select all

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, SysTypesDbContextMigrationConfiguration>()); 
after step 3)

UPDATE: One important thing - due to my program limitation I can't use provider registration via config file, all actions have to be done in code.

Here is my sample code:
http://yadi.sk/d/U69J3nEjCfCAa

INFO:
SQLite Code First
Entity Framework 6.0.1
VS 2010 SP1 (.NET 4.0)
dotConnect for SQLite 5.1.36
Windows 7 SP1 32bit
Last edited by bairog on Tue 17 Dec 2013 12:32, edited 1 time in total.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Code-First automatic migrations problems with dotConnect 5.1.36 and EF 6.0.1 Code-First

Post by Shalex » Mon 16 Dec 2013 13:54

0) enable migrations (remove comment in my sample code near Database.SetInitializer) [by Devart]
1) run my sample code - database is created
2) change entities (remove comment in my sample code to add field)
3) run code again - database is updated [by Devart]
We have sent to the e-mail adress specified in your forum profile a modified test project where this scenario works with enabled automatic migrations.

bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Re: Code-First automatic migrations problems with dotConnect 5.1.36 and EF 6.0.1 Code-First

Post by bairog » Tue 17 Dec 2013 11:41

1) As far as I can see your solution is working becaause of default constructor

Code: Select all

public MyContext()
:base ("MyContext")
{
}
Due to my program limitations I can't use config files at all.
All preparations have to be done in code.
Without config file your project is not working. Exception is thrown inside that default constrictor
Unknown connection string parameter
If I change

Code: Select all

:base ("MyContext")
to for example

Code: Select all

:base ("Data Source = d:\\MyContext.db")
- it's working.
But it is incorrect - connection string may vary..

2) Changing my entity by adding Int32, DateTime? or String - is ok.
But changing it by adding DateTime or Boolean throws an exception
SQLite does not support adding a NOT NULL column while the default value NULL
I can assume it for DateTime, but Boolean is a value type with default value = false...
What is the problem and where I can find more information about SQLite automatic migrations, default values, limitations, etc.
Is there any solution in my situation?

3) You haven't answer my second question: why DbMigrationConfiguration still be applyed even without Database.SetInitializer or with CreateDatabaseIfNotExist, DropCreateDatabaseAlways and DropCreateIfModelChanged.

Thank you.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Code-First automatic migrations problems with dotConnect 5.1.36 and EF 6.0.1 Code-First

Post by Shalex » Tue 17 Dec 2013 16:10

bairog wrote:1) Due to my program limitations I can't use config files at all.
Please refer to http://stackoverflow.com/questions/1550 ... migrations (the approach with AppDomain.CurrentDomain.GetData and AppDomain.CurrentDomain.SetData).
bairog wrote:2) changing it by adding DateTime or Boolean throws an exception
We will investigate the issue and post here about the result.
bairog wrote:3) why DbMigrationConfiguration still be applyed even without Database.SetInitializer or with CreateDatabaseIfNotExist, DropCreateDatabaseAlways and DropCreateIfModelChanged
Please ask this question to Microsoft Support Team at http://entityframework.codeplex.com/discussions.

bairog
Posts: 120
Joined: Mon 29 Apr 2013 09:05

Re: Code-First automatic migrations problems with dotConnect 5.1.36 and EF 6.0.1 Code-First

Post by bairog » Wed 18 Dec 2013 04:45

I've investigated SQLite Code-First Automatic Migrations further:
  1. Adding value-type (Int32, Byte, etc), nullable (DateTime?, Boolean?, etc) or String properties - is ok.
    Except for strange Boolean behavior (see exception in next item) - Devart team is investigating this situation..
  2. Adding reference-type (DateTime, Guid, etc) properties - is ok if property is marked as optional (via DataAnnotations or Fluent API).
    Otherwise exception is raised
    SQLite does not support adding a NOT NULL column with the default value NULL
  3. Changing type for property (Int32 -> Byte) is not supported
    Specified method is not supported
  4. Removing property is not supported
    SQLite does not support dropping a column
  5. Adding new DbSet into context (new table into database) - is ok.
    Foreign key property (where added table's primary key partisipates) should be marked as optional (via DataAnnotations or Fluent API)
    Should mention that for added table's primary key (I've tried for Guid) there is no need to mark it as optional (despite of fact that it is set to NOT NULL by default).
I think the list is not complete, but I've tried most of the situations. Strange that there's no such FAQ or tutorial on site..

Devart, where I can find information about all SQLite migrations limitations to take into account?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Code-First automatic migrations problems with dotConnect 5.1.36 and EF 6.0.1 Code-First

Post by Shalex » Thu 19 Dec 2013 14:27

1,2. The behaviour is changed: Code-First Migrations generates DEFAULT values for new NOT NULL columns of Boolean, Guid, DateTime types when adding them in the existing table. The change will be included in the next build of dotConnect for SQLite. We will post here when it is available for download.

3,4,5. Please refer to http://blog.devart.com/entity-framework ... ionSupport. There is an update about AddForeignKey operation for SQLite: http://forums.devart.com/viewtopic.php?t=28371. We will correct the blog article correspondingly.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Code-First automatic migrations problems with dotConnect 5.1.36 and EF 6.0.1 Code-First

Post by Shalex » Thu 26 Dec 2013 13:39

New build of dotConnect for SQLite 5.1.65 is available for download now!
It can be downloaded from http://www.devart.com/dotconnect/sqlite/download.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=29&t=28598.

Post Reply