Cannot Migrate/Load table with Discriminator.

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for SQLite
Post Reply
Annavelle
Posts: 5
Joined: Fri 22 Jun 2012 02:56

Cannot Migrate/Load table with Discriminator.

Post by Annavelle » Fri 19 Aug 2016 05:20

Now testing migrate to EntityFrameworkCore but...
1. Cannot migrate/generate table with Discriminator
2. Cannot load elements even if database exitsts.

Currently using dotConnect for SQLite v5.6.714 with EntityFrameworkCore 1.0.0.

---sample---
class Program
{
static void Main(string[] args)
{
{
DbContextOptionsBuilder builderDa = new DbContextOptionsBuilder();
builderDa.UseSQLite("DataSource = .test.db");
// builderDa.UseSqlite("DataSource = .test.db");
Context context = new Context(builderDa.Options);
context.Database.Migrate(); /* 1. Cannot migrate/generate database... */
//
context.Add(new EntityExt() { ExtData = "data" });
context.SampleEntity.Add(new EntityExt() { ExtData = "data2" });
context.SaveChanges();
//
context.SampleEntity.Load(); /* 2. Cannot load elements even if database exitsts ... */
}
}
}

public class Context : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityBase>()
.HasDiscriminator()
.HasValue<EntityBase>("base")
.HasValue<EntityExt>("ext")
;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if( !optionsBuilder.IsConfigured)
{
// optionsBuilder.UseSQLite("DataSource=.test.db");
optionsBuilder.UseSqlite("DataSource=.test.db"); /* can generate, but not supports encryption... */
}
}
public Context()
{

}
public Context(DbContextOptions options)
: base(options)
{

}

public DbSet<EntityBase> SampleEntity { get; set; }
}

[Table("EntityBase")]
public class EntityBase
{
[Key]
public int Id { get; set; }
}

public class EntityExt : EntityBase
{
public string ExtData { get; set; }
}

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

Re: Cannot Migrate/Load table with Discriminator.

Post by Shalex » Tue 23 Aug 2016 16:42

Thank you for your report. We will investigate the issues and notify you about the result.

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

Re: Cannot Migrate/Load table with Discriminator.

Post by Shalex » Wed 31 Aug 2016 13:19

This is a designed behaviour caused by EF Core engine, a standard provider (System.Data.SqlClient) throws a similar error:

Code: Select all

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Additional information: Invalid object name 'EntityBase'.
Workaround (via code-based Code-First Migrations):
1. [via Nuget Package Manager Console]
> Install-Package Microsoft.EntityFrameworkCore.SqlServer
2. > Install-Package Microsoft.EntityFrameworkCore.Tools –Pre
3. > add-migration first
4. > update-database
The current public build of dotConnect for SQLite throws System.NullReferenceException when using TPH inheritance in EF Core. The bug is fixed. Look forward to the new public build of dotConnect for SQLite. We will notify you when it is available for download.

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

Re: Cannot Migrate/Load table with Discriminator.

Post by Shalex » Fri 02 Sep 2016 14:08

New build of dotConnect for SQLite 5.6.729 is available for download now: viewtopic.php?f=29&t=34213.

Annavelle
Posts: 5
Joined: Fri 22 Jun 2012 02:56

Re: Cannot Migrate/Load table with Discriminator.

Post by Annavelle » Wed 07 Sep 2016 05:46

I tried dotConnect SQLite 5.6.729 .
Migrate method and update-database completed, but can't load any TPH entities.

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

Re: Cannot Migrate/Load table with Discriminator.

Post by Shalex » Wed 07 Sep 2016 12:59

We have reproduced the issue with the following queries:

Code: Select all

            context.SampleEntity.Load();
            var all_results = context.SampleEntity.ToList();
            var EntityBase_results = context.SampleEntity.OfType<EntityBase>().ToList(); 
We will notify you when it is fixed.

As a workaround, please use:

Code: Select all

            var EntityExt_results = context.SampleEntity.OfType<EntityExt>().ToList(); 

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

Re: Cannot Migrate/Load table with Discriminator.

Post by Shalex » Thu 22 Sep 2016 15:35

The newest (5.6.743) build of dotConnect for SQLite includes the corresponding fixes:
  • The bug with processing IN expressions in EF Core is fixed
  • The bug with retrieving objects of TPH hierarchy in EF Core is fixed
Your code should work now.

Post Reply