Xamarin Forms entity framework core database does not get created

android c# entity-framework-core xamarin xamarin.forms

Question

In xamarin forms, I've been attempting to put up a sqlite database using EF Core. I'm solely interested in making it work on Android at the moment.

What I've already done:

used Init add-migration to create a migration using a dummy console app as the initial project. The datacontext file is located in the same namespace and project as this one.

Autofac initializes DataContext, and the database path is

Filename.db plus System.Environment.SpecialFolder.LocalApplicationData

called DataBase.Migrate() in the constructor for a datacontext

The issue is that if I attempt to access or add anything to the dbset while debugging the program in the android emulator, an error is thrown stating that the table does not exist.

There is no.local folder generated in the application's folder when I check the folder in the Android Files App, and I cannot locate the sqlite file anywhere. To no effect, I also tried setting the path to the personal folder.

The Database and context are correctly initialized while debugging. The Migrate() function operates without any exceptions being raised.

public DataContext(string databasePath)
        {
            DatabasePath = databasePath;
            Database.Migrate();
        }

public DbSet<PageContent> PageContents { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite($"Filename={DatabasePath}");
        }
1
0
3/4/2020 4:00:08 PM

Accepted Answer

After attempting JeePakaJP's solution, it was successful. However, I discovered that it works without having to explicitly create the file using File. Produce as well.

The issue was that for some reason Visual Studio didn't recreate the project.

Additionally, the file is there but is hidden in the file explorer app due to user permissions.

0
3/4/2020 7:15:25 PM

Popular Answer

Try to create a database file.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string databasePath;
            switch (Device.RuntimePlatform)
            {
                case Device.iOS:
                    SQLitePCL.Batteries_V2.Init();

                    string docFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                    string libFolder = Path.Combine(docFolder, "..", "Library", "Databases");

                    if (!Directory.Exists(libFolder))
                    {
                        Directory.CreateDirectory(libFolder);
                    }

                    databasePath = Path.Combine(libFolder, databaseName) ;
                    break;
                case Device.Android:
                    databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), databaseName);
                    break;
                default:
                    throw new NotImplementedException("Platform not supported");
            }

            if (!File.Exists(databasePath))
                File.Create(databasePath);

            // Specify that we will use sqlite and the path of the database here
            optionsBuilder.UseSqlite($"Filename={databasePath}");
        }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow