简体   繁体   English

当我想使用db-migration更新数据库时如何防止数据丢失?

[英]How to prevent data loss when I want to update database by using db-migration?

When I was making update my database by using db-migration, I faced a problem that was 当我使用db-migration更新我的数据库时,我遇到了一个问题

Automatic migration was not applied because it would result in data loss.

(I used System.ComponentModel.DataAnnotations such as [Required] and [StringLength(25)] for some properties. For example Title property.) (对于某些属性,我使用了System.ComponentModel.DataAnnotations ,如[Required][StringLength(25)] 。例如Title属性。)

I know if I set the AutomaticMigrationDataLossAllowed to true and Update-Database -Force , my database will be update but my data will be removed and I'm going to prevent from it. 我知道如果我将AutomaticMigrationDataLossAllowed设置为true并且Update-Database -Force ,我的数据库将会更新,但我的数据将被删除,我将阻止它。 I want to protect my data. 我想保护我的数据。

I've used Entity Framework 6.x 我使用过Entity Framework 6.x.

How can I solve this problem? 我怎么解决这个问题?

Configuration class: 配置类:

namespace Jahan.Blog.Web.Mvc.Migrations
{
   using System;
   using System.Data.Entity;
   using System.Data.Entity.Migrations;
   using System.Linq;

   internal sealed class Configuration 
    : DbMigrationsConfiguration<Jahan.Blog.Web.Mvc.Models.JahanBlogDbContext>
   {
       public Configuration()
       {
           AutomaticMigrationsEnabled = true;
           AutomaticMigrationDataLossAllowed = false;
       }

       protected override void Seed(Jahan.Blog.Web.Mvc.Models.JahanBlogDbContext context)
       {

       }
   }
}

Initial class: 初级课程:

namespace Jahan.Blog.Web.Mvc.Migrations
{
   using System;
   using System.Data.Entity.Migrations;

   public partial class Initial : DbMigration
   {
       public override void Up()
       {
       }

       public override void Down()
       {
       }
   }
}

My DbContext: 我的DbContext:

namespace Jahan.Blog.DataAccess
{
   public class JahanBlogDbContext : IdentityDbContext<User, Role, int, UserLogin, UserRole,    UserClaim>
   {
       public JahanBlogDbContext()
           : base("name=JahanBlogDbConnectionString")
       {

       }
       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<Comment>().HasRequired(t => t.Article).WithMany(t => t.Comments).HasForeignKey(d => d.ArticleId).WillCascadeOnDelete(true);
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>().ToTable("User");
        modelBuilder.Entity<Role>().ToTable("Role");
        modelBuilder.Entity<UserRole>().ToTable("UserRole");
        modelBuilder.Entity<UserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<UserClaim>().ToTable("UserClaim");
       }
       // ... codes ....
   }
}

You can add sql to fix the data in a way that is acceptable to you. 您可以添加sql以您可接受的方式修复数据。 You need to ensure that the alter statements generated BY EF do not cause data loss. 您需要确保BY EF生成的alter语句不会导致数据丢失。

Use the Sql method in a migration to run your own sql: 在迁移中使用Sql方法来运行自己的sql:

public override void Up()
{
    //Add this to your migration...
    Sql("UPDATE dbo.Table SET Name = LEFT(Name, 25) WHERE LEN(Name) > 25")

    //...before the code generated by EF
    AlterColumn("dbo.Table", "Name ", c => c.String(nullable: false, maxLength: 25));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用db-migration确定数据库中字段的值范围 - Determining the range of value for a field in database by using db-migration 当我更新数据时,如何使用CJuiDatePicker从数据库获取数据? - when I update data,how to get data from db with CJuiDatePicker? 如何在没有数据丢失的情况下将数据存储在 database.db 中? - How to store data in database.db w/o data-loss? 我想将数据库表放在同一数据库中。 怎么样? - I want to put db tables in same database. How? 领域迁移时获取过去的数据库数据 - Getting past db data when realm migration 当组件B接收到用户的输入并将其存储在数据库中时,如何更新组件A的状态? 如何从数据库获取A的数据? - How do I update component A's state when component B receives input from user and stores it in database? How do I fetch data for A from DB? Laravel-如何使用工匠迁移将数据迁移到实时数据库? - Laravel - How do I migrate data into a live database using artisan migration? 需要使用数据库链接从另一个数据库更新数据 - Need to update data from another database using db link 我想使用PHP更新数据库表MySQL - I want to update a database table MySQL using PHP 我可以防止在Oracle DB上进行更新或删除吗? - Can I prevent update or delete on an Oracle DB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM