更新SQLite数据库,以反映使用实体框架核心和.NET 6的模型更改

发布于 2025-02-10 13:14:37 字数 823 浏览 2 评论 0原文

我已经创建了一个看起来像这样的dbcontext:

public class DatabaseContext : DbContext
{
    public DbSet<Author> Authors { get; set; }
    public DbSet<Book> Books { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite(@"Data Source=E:\Portfolio\FullStack\HomeLibraryManager\Database\Library.db;");
    }
}

然后我像这样初始化它:

using (var context = new DatabaseContext())
{
    context.Database.EnsureCreated();
    var books = context.Books.ToList();
}

我想在我的book> book类中添加一个变量,但是如何使数据库表添加了该新变量作为一列。

我曾希望nesureCreted()能确保在需要时更新表列,但似乎不是。

任何帮助都很好,

谢谢

编辑解决方案: 我使用context.database.migrate();而不是确定并运行dotnet-ef迁移添加[migrationNamehere]

I've created a dbcontext that looks like this:

public class DatabaseContext : DbContext
{
    public DbSet<Author> Authors { get; set; }
    public DbSet<Book> Books { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite(@"Data Source=E:\Portfolio\FullStack\HomeLibraryManager\Database\Library.db;");
    }
}

And I Initialize it like this:

using (var context = new DatabaseContext())
{
    context.Database.EnsureCreated();
    var books = context.Books.ToList();
}

I would like to add a variable to my Book class but how do I get the database table to have that new variable added as a column.

I had hoped that EnsureCreated() would make sure the table columns were updated if needed but it seems not.

Any help would be great,

thanks

Edit Solution:
I used context.Database.Migrate(); instead of EnsureCreated and ran dotnet-ef migrations add [MigrationNameHere] first

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ人海孤独症 2025-02-17 13:14:37

在软件包管理器控制台中运行add-mogration addurl命令。自您上次迁移以来,Add-Ogration命令检查是否有更改,并带有新的迁移,并带有任何更改。我们可以给迁移一个名字;在这种情况下,我们称迁移为“ addurl”。脚手架代码说,我们需要添加一个可以将字符串数据的URL列添加到DBO.BLOGS表中。如果需要,我们可以编辑脚手架代码,但是在这种情况下不需要。

在软件包管理器控制台中运行update-database命令。此命令将对数据库应用任何待处理的迁移。我们的初始创建迁移已经应用,因此迁移只会应用我们的新addurl迁移。提示:您可以在调用Update-Database时使用–verbose Switch来查看针对数据库执行的SQL。

处理模型更改

Run the Add-Migration AddUrl command in Package Manager Console. The Add-Migration command checks for changes since your last migration and scaffolds a new migration with any changes that are found. We can give migrations a name; in this case we are calling the migration ‘AddUrl’. The scaffolded code is saying that we need to add a Url column, that can hold string data, to the dbo.Blogs table. If needed, we could edit the scaffolded code but that’s not required in this case.

Run the Update-Database command in Package Manager Console. This command will apply any pending migrations to the database. Our InitialCreate migration has already been applied so migrations will just apply our new AddUrl migration. Tip: You can use the –Verbose switch when calling Update-Database to see the SQL that is being executed against the database.

Dealing with Model Changes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文