Entityframework.docs: Document when the SQLite database needs to be configured for copying to the output directory

Created on 9 Apr 2020  Â·  4Comments  Â·  Source: dotnet/EntityFramework.Docs

I'm trying to implement the code based on console application sample but in WPF and I get the following error:

image

I've added the migration and updated the database. This is how my DbContext looks like:

```c#
public class SettingsDbContext : DbContext
{
public DbSet UserSettings { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlite("Data Source=settings.db");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Client1Credentials>(c =>
        {
            c.HasNoKey();
            c.Property(p => p.Username);
            c.Property(p => p.Password);
        });

        modelBuilder.Entity<Client2Credentials>(c =>
        {
            c.HasNoKey();
            c.Property(p => p.Username);
            c.Property(p => p.Password);
        });
    }
}
Here is how my **Model** looks like:

```c#
    public class UserSettings
    {
        public int Id { get; set; }

        public bool RememberCredentials { get; set; }

        [NotMapped]
        public Client1Credentials Client1Credentials { get; set; }

        [NotMapped]
        public Client2Credentials Client2Credentials { get; set; }
    }

Below you can see that the migration and database were created:

image


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

area-migrations area-sqlite

Most helpful comment

Hi @ajcvickers thanks. I figured it out. The trick was to make the .db file to Copy if newer or Copy always in the File properties -> Copy to output directory . I could not see this in the documentation.

All 4 comments

@dgroh It looks like the current directory is different at runtime and hence the app isn't finding the SQLite database file. Try deploying to the current directory of the app at runtime, or being more explicit about the path to the file in the connection string.

Hi @ajcvickers thanks. I figured it out. The trick was to make the .db file to Copy if newer or Copy always in the File properties -> Copy to output directory . I could not see this in the documentation.

Note from team discussion: this step isn't needed for ASP.NET Core, but is for other targets such as WPF.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

divega picture divega  Â·  3Comments

davidliang2008 picture davidliang2008  Â·  4Comments

speciesunknown picture speciesunknown  Â·  3Comments

jaxidian picture jaxidian  Â·  4Comments

MohammadMQ picture MohammadMQ  Â·  3Comments