[英]How to use Database.Migration() after Database.EnsureCreated() EF Xamarin.Forms
I was working on a Xamarin.Forms application and using EF 6 targeting .net standard 2.0 . 我正在开发Xamarin.Forms应用程序,并使用EF 6面向.net standard 2.0 。 The application is live on app store with my DBContext class as follows.
该应用程序通过我的DBContext类在应用程序商店中实时运行,如下所示。
public class DatabaseContext : DbContext
{
private readonly string _databasePath;
// Hotworks related tables
public DbSet<User> User { get; set; }
public DbSet<UserProfile> UserProfile { get; set; }
public DatabaseContext(string databasePath)
{
_databasePath = databasePath;
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Filename={_databasePath}");
}
}
Now, I have to add a new column to the User Table. 现在,我必须在用户表中添加一个新列。 For that, I have created a .net core console application and generated the migration folder using below commands.
为此,我创建了一个.net核心控制台应用程序,并使用以下命令生成了迁移文件夹。
dotnet ef migrations add Initial
dotnet ef update database
Then, I copied the generated Migration folder to my dbContext project and changed the Database.EnsureCreated() to Database.Migration() But, I'm getting SQLite error 1: Table "User" already exists . 然后,将生成的Migration文件夹复制到我的dbContext项目中,并将Database.EnsureCreated()更改为Database.Migration()但是,我得到了SQLite错误1:表“ User”已经存在 。 Any help?
有什么帮助吗? Code already in the production having Database.EnsureCreated() but now I have to migrate and update the database but, it is not working and always I'm getting the table already exist error.
生产中的代码已经具有Database.EnsureCreated(),但是现在我必须迁移和更新数据库,但是它无法正常工作,并且总是让我得到表已存在的错误。
Here's a safer approach compared to my previous comment 与我之前的评论相比,这是一种更安全的方法
add-migration Initial
for the models that is equivalent to the production database (ie without the newly added column in User
table and other changes that you did after deploying to production). add-migration Initial
等效于生产数据库的模型的add-migration Initial
值(即,在User
表中没有新添加的列,以及您部署到生产后所做的其他更改)。 __EFMigrationsHistory
to include the Initial
migration. __EFMigrationsHistory
以包括Initial
迁移。 add-migration XXX
for the updated schema (ie new columns, etc). add-migration XXX
。 XXX
should then be applied to your production database by the Database.EnsureMigrate()
method. Database.EnsureMigrate()
方法将XXX
应用于生产数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.