Page 1 of 1

Cannot Migrate/Load table with Discriminator.

Posted: Fri 19 Aug 2016 05:20
by Annavelle
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; }
}

Re: Cannot Migrate/Load table with Discriminator.

Posted: Tue 23 Aug 2016 16:42
by Shalex
Thank you for your report. We will investigate the issues and notify you about the result.

Re: Cannot Migrate/Load table with Discriminator.

Posted: Wed 31 Aug 2016 13:19
by Shalex
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.

Re: Cannot Migrate/Load table with Discriminator.

Posted: Fri 02 Sep 2016 14:08
by Shalex
New build of dotConnect for SQLite 5.6.729 is available for download now: viewtopic.php?f=29&t=34213.

Re: Cannot Migrate/Load table with Discriminator.

Posted: Wed 07 Sep 2016 05:46
by Annavelle
I tried dotConnect SQLite 5.6.729 .
Migrate method and update-database completed, but can't load any TPH entities.

Re: Cannot Migrate/Load table with Discriminator.

Posted: Wed 07 Sep 2016 12:59
by Shalex
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(); 

Re: Cannot Migrate/Load table with Discriminator.

Posted: Thu 22 Sep 2016 15:35
by Shalex
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.