# 9.2.1 Creando nuestra tabla roles e insertando los roles principales

Vamos a crear nuestra tabla **rol**, la cual sólo contendrá el campo **Id** y **Nombre**.

{% code title="Rol.cs" %}

```csharp
public class Rol
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required(ErrorMessage = "Required")]
    [Column(TypeName = "VARCHAR(50)")]
    public string Nombre { get; set; }
}
```

{% endcode %}

Los inserts los vamos a agregar en nuestra clase **CaducaContext** en el método **OnModelCreating**, agregamos al **modelBuilder** la propiedad **Entity** y le pasamos de cual tabla deseamos agregar datos, en estaecaso **rol**, luego en el método HasData agregamos los 3 roles que necesitamos, "**Administrador**", "**Vendedor**", "**Cliente**". De esta forma estos inserts se agregan a la migración si luego cambiamos el nombre de un campo se realizará un Update.

Recuerda también agregar la entidad Rol

{% code title="CaducaContex.cs" %}

```csharp
public class CaducaContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Rol>().HasData(
               new Rol { Id = 1,  Nombre = "Administrador" },
               new Rol { Id = 2,  Nombre = "Vendedor" },
               new Rol { Id = 3,  Nombre = "Cliente" },
               new Rol { Id = 4,  Nombre = "Supervisor" }
        );     
    }
    public virtual DbSet<Rol>  Rol { get; set; }
}
```

{% endcode %}

Creamos una nueva migración TablaRol

Comando en Windows

```
Add-Migration TablaRol
```

Comando en mac

```
dotnet ef migrations add TablaRol
```

Si revisamos el archivo de migración verás que se agrega las funciones InsertData para realizar los insert a nuestra tabla rol

{% code title="20190531032036\_TablaRol.cs" %}

```csharp
public partial class TablaRol : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
       migrationBuilder.CreateTable(
           name: "Rol",
           columns: table => new
           {
              Id = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", 
                        MySqlValueGenerationStrategy.IdentityColumn),
                    Nombre = table.Column<string>(type: "VARCHAR(50)", 
                        nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Rol", x => x.Id);
                });

        migrationBuilder.InsertData(
                table: "Rol",
                columns: new[] { "Id", "Nombre" },
                values: new object[] { 1, "Administrador" });

        migrationBuilder.InsertData(
                table: "Rol",
                columns: new[] { "Id", "Nombre" },
                values: new object[] { 2, "Vendedor" });

         migrationBuilder.InsertData(
                table: "Rol",
                columns: new[] { "Id", "Nombre" },
                values: new object[] { 3, "Cliente" });
        
         migrationBuilder.InsertData(
                table: "Rol",
                columns: new[] { "Id", "Nombre" },
                values: new object[] { 4, "Supervisor" });
                
                
}
```

{% endcode %}

Actualizamos la base de datos

Comando en windows

```
Update-database
```

Comando en mac

```
dotnet ef database update
```

Listo hemos creado nuestros primeros inserts con migraciones


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://abi.gitbook.io/net-core/7.-seguridad/7.2-seguridad-basada-en-roles/7.3-creando-nuestra-tabla-roles-e-insertando-los-3-roles-principales.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
