请输入您要查询的百科知识:

 

词条 Database seeding
释义

  1. Entity Framework

  2. Symfony PHP Framework

  3. Laravel PHP Framework

  4. External links

Database seeding is the initial seeding of a database with data.

Seeding a database is a process in which an initial set of data is provided to a database when it is being installed .

It is especially useful when we want to populate the database with data we want to develop in future.

This is often an automated process that is executed upon the initial setup of an application.

The data can be dummy data or necessary data such as an initial administrator account.

Entity Framework

{{Main|Entity Framework}}

\\Migrations\\Configuration.cs

public class ApplicationDatabaseInitializer : DropCreateDatabaseIfModelChanges

{
    protected override void Seed(DbContext context)    {        var UserManager = new UserManager(new UserStore(context));        var RoleManager = new RoleManager(new RoleStore(context));
        var username = "Alice";        var password = "password123";        var role = "Admin";
        // Create role Admin if it does not exist        if (!RoleManager.RoleExists(role))        {            RoleManager.Create(new IdentityRole(role));        }
        // Create user Alice        var user = new ApplicationUser() { UserName = username; };        var result = UserManager.Create(user, password);
        // Add user Admin to role Admin        if (result.Succeeded)        {            var result = UserManager.AddToRole(user.Id, role);        }    }

}

Symfony PHP Framework

{{Main|Symfony}}

AppBundle/DataFixtures/ORM/customer.yml (as in Version 1 of [https://github.com/hautelook/AliceBundle hautelook/AliceBundle] )

AppBundle\\Entity\\User:

  customer_{1..10}:    username:     email:     plainPassword: theLetterA    roles: [ROLE_SUPER_ADMIN]    enabled: true

Laravel PHP Framework

{{Main|Laravel}}

app/database/seeds/users.php

class DatabaseSeeder extends Seeder {

    public function run()    {        $this->call('UserTableSeeder');        $this->command->info('User table seeded!');    }

}

class UserTableSeeder extends Seeder {

    public function run()    {        DB::table('users')->delete();        User::create(array('email' => 'foo@bar.com'));    }

}

External links

  • http://laravel.com/docs/migrations
  • https://msdn.microsoft.com/en-us/library/gg679410%28v=vs.113%29.aspx

1 : Databases

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/17 7:58:53