简体   繁体   English

如何在Database.EnsureCreated()和EF Xamarin.Forms之后使用Database.Migration()

[英]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 与我之前的评论相比,这是一种更安全的方法

  1. 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表中没有新添加的列,以及您部署到生产后所做的其他更改)。
  2. Manually update the production database __EFMigrationsHistory to include the Initial migration. 手动更新生产数据库__EFMigrationsHistory以包括Initial迁移。
  3. add-migration XXX for the updated schema (ie new columns, etc). 为更新的架构(即新列等) add-migration XXX
  4. 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.

相关问题 Database.EnsureCreated似乎不是在创建数据库(xamarin.forms) - Database.EnsureCreated does not seem that it's creating a database (xamarin.forms) Database.EnsureCreated无法正常工作 - Database.EnsureCreated not working 我应该把Database.EnsureCreated放在哪里? - Where should I put Database.EnsureCreated? EF Core Database.EnsureCreated 获取创建表 SQL 并且不向数据库发送请求 - EF Core Database.EnsureCreated get creating table SQL and without sending request to database Database.EnsureCreated EF 7 ASP.NET 5 MVC 6处的堆栈溢出异常 - Stack Overflow exception at Database.EnsureCreated EF 7 ASP.NET 5 MVC 6 EntityFramework Core Database.EnsureCreated不会创建数据库 - EntityFramework Core Database.EnsureCreated doesn't create database 如何使用 .NET 6 在 aspnet 核心 Web 应用程序中执行 database.ensurecreated()? - How do you do database.ensurecreated() in aspnet core web application using .NET 6? 实体框架核心 - Database.Migration() - Entity Framework Core - Database.Migration() 如何从Xamarin.Forms上的SQLite PCL数据库中删除项目? - How to delete item from SQLite PCL database on Xamarin.Forms? 数据库 sqlite 中的项目未更新 (Xamarin.Forms) - Items in database sqlite are not updating (Xamarin.Forms)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM