How do you put the database on?



  • There's a local whale of a river that's going to decorate the hosting, how can the database be similarly demolished?



  • Normally, no one copies a database from one server to another, including because test data are used in the developers ' database and the combat database is real, which cannot be rewritten.

    In fact, it is necessary for developers to change the OBD scheme and to provide background data (country lists, city telephone codes, etc.). The migration mechanism is used.

    Under different platforms, this can be done differently. I'll tell you from my experience how this is done in Entity Framework.

    First, in order to avoid duplication, the structure of the base is not stored in the form of DDL scruples (in order to avoid duplication).CREATE TABLE and other) and classes .NET.

    [Table("Users")]
    public class User
    {
        [Key]
        public Guid Id { get; set; }
    
    public string Login { get; set; }
    
    [MaxLength(512)]
    public byte[] PasswordHash { get; set; }
    

    }

    All tables that are included in the OBD are listed in the assigned class, which is known as database context:

    public class BlogDbContext : DbContext
    {
    public DbSet<User> Users { get; set; }
    }

    At the beginning of the work, we need to create the first BD Blind for work (first migration). In fact, the Entity Framework code is investigating BlogDbContext and User and prepare violators to generate the table Users and the necessary indices. PowerShell-comanda is used to create migration Add-Migration

    Add-Migration Initial

    The violette will look like this:

    public partial class Initial : DbMigration
    {
    public override void Up()
    {
    CreateTable(
    "dbo.Users",
    c => new
    {
    Id = c.Guid(nullable: false),
    Login = c.String(nullable: false),
    PasswordHash = c.ByteArray(maxlength: 512),
    })
    .PrimaryKey(t => t.Id);
    }

    public override void Down()
    {
        DropTable("dbo.Users");
    }
    

    }

    As you can see, the crypt comprises two branches: to move forward on the list of migrations (method) Up) and to roll back (method) Down)

    Now, please note, the OBD scheme is part of the entire rest of the project reference code and is distributed with it.

    If we need to change the scheme, we do it, for example, add a new class tablet or a new field, and we start again. Add-Migration♪ Entity Framework generates again a class that updates the scheme. In the vast majority of cases, you won't need to manually rule the code, but you can do it, for example, if you need a non-standard index.

    What now? You publish your code on the combat server. In the next address to your server, the diagram is checked and, if necessary, all updates to the diagram are rolled out transparently. Up)

    As a result, the database is being upgraded with all the other codes and, by the way, not only on the combat server, but also between the developers.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2