Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Add table to existing sqllite database using EFCore.

$
0
0

Hello,

public class AppContext: DbContext
{
    private readonly string _databasePath;

        public AppContext()
        {
            _databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "App.db");
            Database.EnsureCreated();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite($"Filename={_databasePath}");
        }
    public DbSet<Product>    Product{ get; set; }
        public DbSet<Category> Category{ get; set; }
}

Initially above code works fine and it will create a database.

    public DbSet<Product> Product{ get; set; }
        public DbSet<Category> Category{ get; set; }
    public DbSet<Customer> Customer{ get; set; }

But if I added a new model then I have to first delete a database and then create a new database like below code. By using this approach I lost my all data.

    public AppContext()
        {
            _databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "App.db");
        Database.EnsureDeleted();
            Database.EnsureCreated();
        }

So, is there any way to add new table sqllite database without deleting existing data.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>