Database seeding: Difference between revisions
Appearance
Content deleted Content added
mNo edit summary Tags: Mobile edit Mobile web edit |
m Fixes broken link |
||
Line 82: | Line 82: | ||
== External links == |
== External links == |
||
* http://laravel.com/docs/migrations |
* http://laravel.com/docs/migrations |
||
* https:// |
* https://docs.microsoft.com/en-us/previous-versions/gg679410(v%3Dvs.113) |
||
[[Category:Databases]] |
[[Category:Databases]] |
Revision as of 05:13, 15 May 2019
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
\Migrations\Configuration.cs
public class ApplicationDatabaseInitializer : DropCreateDatabaseIfModelChanges<DbContext>
{
protected override void Seed(DbContext context)
{
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(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
AppBundle/DataFixtures/ORM/customer.yml (as in Version 1 of hautelook/AliceBundle )
AppBundle\Entity\User:
customer_{1..10}:
username: <username()>
email: <safeEmail()>
plainPassword: theLetterA
roles: [ROLE_SUPER_ADMIN]
enabled: true
Laravel PHP Framework
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'));
}
}