C#在使用EntityFramework(Sqlite)修改数据库时出现异常。

huangapple go评论63阅读模式
英文:

C# exception when modifying database with EntityFramework (Sqlite)

问题

应用程序崩溃并显示以下异常信息:
Microsoft.EntityFrameworkCore.DbUpdateException
  Message=在保存实体更改时发生错误。请查看内部异常以获取详细信息。
  Source=Microsoft.EntityFrameworkCore.Relational

内部异常 1:
SqliteException: SQLite错误 1: '没有这样的表: Blogs'。

要解决此问题,您需要确保数据库已正确初始化和迁移,以便包含名为 "Blogs" 和 "Posts" 的表。您可以执行以下步骤:

  1. 打开终端或命令提示符,并导航到您的项目目录。

  2. 运行以下命令以创建数据库迁移:

    dotnet ef migrations add InitialCreate
    

    这将生成用于创建数据库表的迁移文件。

  3. 然后运行以下命令以应用迁移并创建数据库表:

    dotnet ef database update
    

    这将在您的 SQLite 数据库中创建 "Blogs" 和 "Posts" 表。

  4. 重新运行您的应用程序,看看是否仍然出现问题。

这些步骤应该解决 "no such table: Blogs" 的问题,确保数据库已正确配置和迁移。如果问题仍然存在,请确保您的连接字符串和数据上下文配置正确,并且SQLite 包已正确安装。希望这对您有所帮助!

英文:

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.

答案1

得分: 0

内部异常显示了真正的问题:“SqliteException: SQLite错误1:'没有这个表:Blogs'。”。所以它表示在你的数据库中表“blogs”不存在。Entity Framework 的一个关键概念是迁移。

当引入数据模型更改时,开发人员使用EF Core工具添加相应的迁移,描述保持数据库架构同步所需的更新。(来源)

所以你需要创建一个新的迁移(如果你还没有这样做),然后更新数据库。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月8日 22:59:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定