Home > OS >  C# Entity Framework Core write into sqlite_sequence Table
C# Entity Framework Core write into sqlite_sequence Table

Time:06-24

I´ve got a SQLite-Db and using EF Core. We need to save a sequence of DateTimes, where each time a continously process calls the controller the timespan from last request to DateTime.Now is the process relevant data.

Therefor I wanted to save the last DateTime into the sequence table of SQLite but on Database.EnsureCreated() I get the following exception SQLite Error 1: 'object name reserved for internal use: sqlite_sequence'. I think its because EF tries to create the table but should not.

Does someone know how to write custom data into this table or someone has a better way to store a single DateTime within the Database?

CodePudding user response:

        I will give your this approach for ef to sql lite for your db context and using this will easy to store any data (Db configurations is used to configure properties in the table).
    
            public class ApplicationContext : DbContext
                {
                    protected override void 
 OnConfiguring(DbContextOptionsBuilder optionsBuilder)
                    {
                        optionsBuilder.UseSqlite("Filename=database.db", options =>
                        {
                            
        options.MigrationsAssembly(Assembly.GetExecutingAssembly().FullName);
                        });
                        if (!optionsBuilder.IsConfigured)
                        {
                            optionsBuilder.UseSqlite("Filename=database.db");
                        }
                        base.OnConfiguring(optionsBuilder);
                    }
                    protected override void OnModelCreating(ModelBuilder modelBuilder)
                    {
                        modelBuilder.ApplyConfiguration(new SettingsDbConfiguration());
                        base.OnModelCreating(modelBuilder);
                    }
                    
                    public DbSet<BaseSettings> Settings { get; set; }
               
    
     }
  • Related