0

developers. I'm a begginer so please excuse me if I don't know how to explain. So I have this code sample into another class:

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    public string DbPath { get; }

    public BloggingContext()
    {
        var folder = Environment.SpecialFolder.LocalApplicationData;
        var path = Environment.GetFolderPath(folder);
        DbPath = System.IO.Path.Join(path, "blogging.db");
    }

    // The following configures EF to create a Sqlite database file in the
    // special "local" folder for your platform.
    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlite($"Data Source={DbPath}");
}
 

public class Blog
{
    public int BlogId { get; set; }
    public string? Url { get; set; }

    public List<Post> Posts { get; } = new();
}

public class Post
{
    public int PostId { get; set; }
    public string? Title { get; set; }
    public string? Content { get; set; }

    public int BlogId { get; set; }
    public Blog? Blog { get; set; }
}

All i want is to modify the database using Sqlite, this is my Main method:

using var db = new BloggingContext();
Console.WriteLine($"Database Path: {db.DbPath}");
//Create
Console.WriteLine("Insert a new blog");
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();
//Read
Console.WriteLine("Querying for a blog");
var blog = db.Blogs
    .OrderBy(b => b.BlogId)
    .First();
//Update
Console.WriteLine("Updating the blog");
blog.Url = "www.blablabla.com";
blog.Posts.Add(new Post { Title = "My first post", Content = "Playing with EntityFramework" });
db.SaveChanges();
//Delete
Console.WriteLine("Deleting what we created");
db.Remove(blog);
db.SaveChanges();

However, the app crashes and it's giving me the following exception: Microsoft.EntityFrameworkCore.DbUpdateException Message=An error occurred while saving the entity changes. See the inner exception for details. Source=Microsoft.EntityFrameworkCore.Relational

Inner Exception 1: SqliteException: SQLite Error 1: 'no such table: Blogs'.

I would like to know how to fix the issue, I've been looking everywhere but I couldn't find anything wrong. Any advice is helpful. Thanks a lot!

Tried installing all types of packages, nothing worked.

0

1 Answer 1

0

The inner exception says what's the real problem: "SqliteException: SQLite Error 1: 'no such table: Blogs'.". So it says that the table "blogs" doesn't exist in your database. A key concept of entity framework are migrations.

When a data model change is introduced, the developer uses EF Core tools to add a corresponding migration describing the updates necessary to keep the database schema in sync. (source)

So you need to create a new migration (if you haven't done that already) and update the database.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.