简体   繁体   中英

Xamarin.Forms doesn't find migrations of EntityFramework.Core

I Am wondering what i am doing wrong because EnsureCreated() works as i want but Migrate() doesn't work because no migrations are ever found.

Here is some code:

public SqliteContext(string FileName)
{
   this.FileName = FileName;
   Database.EnsureCreated();
}

Works

But when i do the same but with:

public SqliteContext(string FileName)
{
   this.FileName = FileName;
   Database.Migrate();
}

It doesn't. So i checked and it seems no migrations are ever found. But i have my migrations in the same project as my SqliteContext/dbContext. (In map Migrations) So i assume i don't have to specify a migration assembly but i did that as well but it still didn't work.

For some extra information:

My migrations are generated by Microsoft.EntityFrameworkCore.Tools.

I am using:

Microsoft.EntityFrameworkCore.Tools 3.1.12
Microsoft.EntityFrameworkCore.Sqlite 3.1.12
Xamarin.Forms 5.0.0.2012
Xamarin.Essentials 1.6.1
FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "narco.db"

Seems i found the solution.

public class DbMigrationContext : SqliteContext
    {
        public DbContext() : base(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "narco.db"))
        {
            
        }
    }

I added this class to my Core project so if there were any changes in my context i use in my project they auto reflect in the context i use to create my migrations.

    [DbContext(typeof(DbMigrationContext))]

Is put on top of my migration so its not found. So when i changed it to

    [DbContext(typeof(SqliteContext))]

It now works as expected. So i guess from now on i will just disconnect the reference to my project with my SqliteContext and just copy it over to avoid issues like this.

Hopefully whoever has this issue will find this useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM