radimitrov / CSharpShellApp

79 stars 18 forks source link

Ef core bug #134

Open kovlevi opened 2 years ago

kovlevi commented 2 years ago

I just builded an apk (xamarin forms) with ef core and it throw the foolowing exception: Screenshot_20220508_160544_com companyname gothamstrategy

The code within my db context:

public GothamDbContext() {

          //DbSets
        path = Path.Combine(firstPath,"data.db3");
        //Database.EnsureDeleted();
        Database.EnsureCreated();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite($"Filename={path}");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CourtTrialScene>().Property(x => x.Questions).HasConversion(x => Serialize(x),x => Deserialize<List<string>>(x));
        modelBuilder.Entity<FightScene>(x => 
        {
            x.Property(x => x.PoliceLocation).HasConversion(x => Serialize(x),x => Deserialize<List<int>>(x));
            x.Property(x => x.EnemiesLocation).HasConversion(x => Serialize(x),x => Deserialize<List<int>>(x));
        });
        modelBuilder.Entity<InterrogationScene>().Property(x => x.Questions).HasConversion(x => Serialize(x),x => Deserialize<List<string>>(x));
        modelBuilder.Entity<Villain>().Property(x => x.Aliases).HasConversion(x => Serialize(x),x => Deserialize<List<string>>(x));
        modelBuilder.Entity<Work>().HasData(new WorkSeeder().Seed());
        modelBuilder.Entity<Armor>().HasData(new ArmorSeeder().Seed());
        modelBuilder.Entity<Weapon>().HasData(new WeaponSeeder().Seed());

    }

    string Serialize<T>(T input)
    {
        return JsonSerializer.Serialize(input,default);
    }

    T Deserialize<T>(string json)
    {
        return JsonSerializer.Deserialize<T>(json,default);
    }

This happens in debug with seeding(hasdata in Onmodelcreating, but I think it's not important) even if I uncomment Database.EnsureDeleted():

kovlevi commented 2 years ago

Screenshot_20220508_161634_com radinc csharpshell

kovlevi commented 2 years ago

Perhaps the problem is this, but I don't know the reason and it's fix. Please help me!

kovlevi commented 2 years ago

These are the pictures about the problem:

Screenshot_20220508_170900_com radinc csharpshell

kovlevi commented 2 years ago

Screenshot_20220508_170847_com radinc csharpshell

(I'm using init on a property)

radimitrov commented 2 years ago

I'm not sure about the entity error. Is it within C# Shell only? As in not when run as an independent app? The initializer error is the property "init", I think. I'll add this as a workaround for now. You can try it too.

kovlevi commented 2 years ago

And what about the first screenshot problem: the type initalizer 'Microsoft.EntityFrameworkCore.Internal.CoreStrings' threw exception(this happens only in builded apk, about entity error because of this error can't say anything if that would work in the builded(standalone) apk)? Do you have any idea about that why it happens or how can I solve it?

radimitrov commented 2 years ago

Maybe it is bundling a newer version of EntityFrameworkCore instead of the embedded one? I'll check. For now the app supports only 2.2.

radimitrov commented 2 years ago

No, just checked. It bundles into the APK what it is using - v2.2.

kovlevi commented 2 years ago

Yeah, I know and I have that 2.2.0 nuget package:

Screenshot_20220508_214203_com radinc csharpshell

radimitrov commented 2 years ago

Then perhaps storage permissions? What is the root path?

kovlevi commented 2 years ago

FileSystem.AppDataDirectory - from xamarin essentials, this is where it creates the database(db3) file and I have storage permissions

Screenshot_20220508_214846_com radinc csharpshell

radimitrov commented 2 years ago

Then the problem isn't path or permissions.

kovlevi commented 2 years ago

the type initalizer 'Microsoft.EntityFrameworkCore.Internal.CoreStrings' threw exception - this is the exception that it throws/shows

radimitrov commented 2 years ago

Well, the exception makes some sense now. Notice the missing native library in the APK? I'll fix that tomorrow. eftest_apk

kovlevi commented 2 years ago

Yeah, I see that your test app didn't generated that selected file. Thank you, I'm waiting for the update then

radimitrov commented 2 years ago

It looks like an issue in the dependency resolver. In case I don't manage to fix it tomorrow then manually referencing this library should do it. sqlite_lib

kovlevi commented 2 years ago

Tried it, but it still throws the same exception

radimitrov commented 2 years ago

Maybe there is something else too. But make sure the lib is actually in APK. apk_libs

kovlevi commented 2 years ago

Yeah, I see it inside(I renamed .apk to .zip to see it):

Screenshot_20220508_225711_com huawei filemanager

radimitrov commented 2 years ago

Ok, at least that works. I will look over the dependency resolver tomorrow. Maybe the wrong framework version DLL for something is being embedded.

kovlevi commented 2 years ago

All right. Anyway thanks for your help

kovlevi commented 2 years ago

I think I found the problem. Look for this file(this path was in innerexception message):

Screenshot_20220508_233152_com my eftest

I don't see this file

radimitrov commented 2 years ago

It was mainly the dependency resolvers losing the scent. Should be working as expected in the next update.

kovlevi commented 2 years ago

When will you release the next update?

radimitrov commented 2 years ago

Probably tomorrow. I need to make sure nothing was broken by this change.

kovlevi commented 2 years ago

All right, thank you for your help

kovlevi commented 2 years ago

Updated C# Shell, but the same exception still throws and that assemblies/Microsoft.EntityFrameworkCore.resources.dll still not generated

radimitrov commented 2 years ago

Yes, I can see the error for the first time on my phone. I'll test an idea on how to fix it.

kovlevi commented 2 years ago

That exception is fixed with the latest update. But now this is the new Exception it throws:

Screenshot_20220510_123447_com companyname eftest

This is my test code by the way:

using System; using System.IO; using Xamarin.Forms; using Microsoft.EntityFrameworkCore.Sqlite; using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations; using Xamarin.Essentials;

namespace EfTest { public partial class App : Application { public App() { InitializeComponent(); MainPage = new MainPage(); try { PersonDbContext context = new PersonDbContext(); Show("ok"); } catch(Exception ex) { Show(ex.Message); } }

    async void Show(string s)
    {
       await App.Current.MainPage.DisplayAlert("",s,"OK");
    }
}

class Person
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id{get;set;}
    public string Name{get;set;}
}

class PersonDbContext : DbContext
{
    public DbSet<Person> People{get;set;}

    public PersonDbContext()
    {
        Database.EnsureCreated();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=/storage/emulated/0/data.db3");
    }
}

}

radimitrov commented 2 years ago

Yeah, I have it too. Appears to happen every time EntityFrameworkCore wants to throw an Exception or log anything. Not sure what the root cause is. This might take longer to fix than expected.

kovlevi commented 2 years ago

All right then I will wait for the fix, hope you can fix all ef core bugs

kovlevi commented 2 years ago

It seems to be working now. Thank you very much for your help.