更新SQLite数据库,以反映使用实体框架核心和.NET 6的模型更改
我已经创建了一个看起来像这样的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
处理模型更改
Dealing with Model Changes