Porting project from Entity Framework 6 (.NET 4.0) to Entity Framework Core (.NET Core 2.0)

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

Porting project from Entity Framework 6 (.NET 4.0) to Entity Framework Core (.NET Core 2.0)

Post by bairog » Wed 04 Apr 2018 12:28

Hello.
For today we have project which uses Entity Framework 6 Code-First approach (.NET 4.0).
I'm trying to port this project to Entity Framework Core (.NET Core 2.0) and I faced several problems:
  1. In EF 6 I used configuration with DbConfigurationTypeAttribute which is missing from EF Core:

    Code: Select all

    [DbConfigurationType(typeof(Devart.Data.SQLite.Entity.SQLiteEntityProviderServicesConfiguration))]
  2. In EF 6 to read user_version I used the access to ObjectContext which is missing from EF Core:

    Code: Select all

    var objContext = ((IObjectContextAdapter)MyDbContext).ObjectContext;
    var user_version = objContext .ExecuteStoreQuery<long>("PRAGMA [main].user_version").FirstOrDefault();
  3. In EF 6 I programmatically added data provider using SQLiteConnectionInfo.InvariantName which is missing from Devart.Data.SQLite.Entity.EFCore:

    Code: Select all

    try
    {
    var dataTable = (ConfigurationManager.GetSection("system.data") as DataSet).Tables[0];
    
    //DbProviderFactory is already installed - need to delete it before
    foreach (DataRow row in dataTable.Rows)
      if (row[2].Equals(SQLiteConnectionInfo.InvariantName))
      {
          row.Delete();
          row.AcceptChanges();
          break;
       }
    
       //Add Data Provider
       dataTable.Rows.Add("dotConnect for SQLite",
       "dotConnect for SQLite",
       SQLiteConnectionInfo.InvariantName,
       "Devart.Data.SQLite.SQLiteProviderFactory, Devart.Data.SQLite, Culture=neutral, PublicKeyToken=09af7300eec23701");
    }
    catch (Exception ex)
    {
         throw new Exception("DataProvider add error", ex);
    }
  4. In EF 6 I used SQLiteEntityMigrationSqlGenerator which is missing from Devart.Data.SQLite.Entity.EFCore:

    Code: Select all

    internal sealed class MyDbContextMigrationConfiguration : DbMigrationsConfiguration<MyDbContext>
    {
         public MyDbContextMigrationConfiguration()
         {
              AutomaticMigrationsEnabled = true;
              AutomaticMigrationDataLossAllowed = false;
              SetSqlGenerator(SQLiteConnectionInfo.InvariantName, new SQLiteEntityMigrationSqlGenerator());
          }
    }
    ........................................................
    //migration settings
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, MyContextMigrationConfiguration>());
So what are the workarounds for that problems?
Thank you in advance.

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

Re: Porting project from Entity Framework 6 (.NET 4.0) to Entity Framework Core (.NET Core 2.0)

Post by Shalex » Thu 05 Apr 2018 14:48

We recommend you to recreate EF Core model from scratch using Entity Developer instead of migrating EF6 to EF Core.


Answers to your questions are:
bairog wrote: Wed 04 Apr 2018 12:281. In EF 6 I used configuration with DbConfigurationTypeAttribute which is missing from EF Core:

Code: Select all

[DbConfigurationType(typeof(Devart.Data.SQLite.Entity.SQLiteEntityProviderServicesConfiguration))]
EF Core picks up our provider via the optionsBuilder.UseSQLite extension method.

bairog wrote: Wed 04 Apr 2018 12:282. In EF 6 to read user_version I used the access to ObjectContext which is missing from EF Core:

Code: Select all

var objContext = ((IObjectContextAdapter)MyDbContext).ObjectContext;
var user_version = objContext .ExecuteStoreQuery<long>("PRAGMA [main].user_version").FirstOrDefault();
You can get an ADO.NET connection via context.Database.GetSQLiteConnection() and run your command via SQLiteCommand.ExecuteScalar().

bairog wrote: Wed 04 Apr 2018 12:283. In EF 6 I programmatically added data provider using SQLiteConnectionInfo.InvariantName which is missing from Devart.Data.SQLite.Entity.EFCore:

Code: Select all

try
{
var dataTable = (ConfigurationManager.GetSection("system.data") as DataSet).Tables[0];

//DbProviderFactory is already installed - need to delete it before
foreach (DataRow row in dataTable.Rows)
  if (row[2].Equals(SQLiteConnectionInfo.InvariantName))
  {
      row.Delete();
      row.AcceptChanges();
      break;
   }

   //Add Data Provider
   dataTable.Rows.Add("dotConnect for SQLite",
   "dotConnect for SQLite",
   SQLiteConnectionInfo.InvariantName,
   "Devart.Data.SQLite.SQLiteProviderFactory, Devart.Data.SQLite, Culture=neutral, PublicKeyToken=09af7300eec23701");
}
catch (Exception ex)
{
     throw new Exception("DataProvider add error", ex);
}
Please specify "Devart.Data.SQLite" explicitly.

bairog wrote: Wed 04 Apr 2018 12:284. In EF 6 I used SQLiteEntityMigrationSqlGenerator which is missing from Devart.Data.SQLite.Entity.EFCore:

Code: Select all

internal sealed class MyDbContextMigrationConfiguration : DbMigrationsConfiguration<MyDbContext>
{
     public MyDbContextMigrationConfiguration()
     {
          AutomaticMigrationsEnabled = true;
          AutomaticMigrationDataLossAllowed = false;
          SetSqlGenerator(SQLiteConnectionInfo.InvariantName, new SQLiteEntityMigrationSqlGenerator());
      }
}
........................................................
//migration settings
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, MyContextMigrationConfiguration>());
MigrateDatabaseToLatestVersion is no longer available in EF6, you should use context.Database.Migrate().

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

Re: Porting project from Entity Framework 6 (.NET 4.0) to Entity Framework Core (.NET Core 2.0)

Post by bairog » Fri 06 Apr 2018 09:10

Shalex wrote: Thu 05 Apr 2018 14:48 We recommend you to recreate EF Core model from scratch using Entity Developer instead of migrating EF6 to EF Core.
What do you mean by that phrase? I use Code-First approach - so my model is a set if POCO classes..

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

Re: Porting project from Entity Framework 6 (.NET 4.0) to Entity Framework Core (.NET Core 2.0)

Post by Shalex » Fri 06 Apr 2018 15:53

If you didn't use *.edml, there is no need to use *.efml.

Post Reply