Add record to table by default in ASP.NET MVC with Code-First

1

Someone who works with the CodeFirst model in ASP.NET MVC and can give me an idea to know: How to add a user by default in the User table >, when executing the construction of the tables of my Context for the first time? I put this simple fragment of 3 tables that are created in the project. The one I'm interested in is the User table, because it's where I want to create ONE user (ADMIN), immediately after the table is created. Note that the user_id_created column must be REQUIRED , so that ONLY subsequent records will store which user created them. I hope to be the clearest and simplest to give me to understand what I want to do. I thank you in advance for the interest and / or help.

class Project_Context : DbContext
{
    public Project_Context() : base("DefaultConnection")
    {
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
    public DbSet<State> States { set; get; }
    public DbSet<City> Cities { set; get; }
    public DbSet<User> Users { set; get; }
}
public class User
{
    [Column("user_id")]
    [Key]
    public int Id { set; get; }
    [Column("user_email")]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "E-mail")]
    [Index("user_user_email", IsClustered = false, IsUnique = false, Order = 1)]
    [StringLength(100, ErrorMessage = "El campo {0} puede contener máximo {1} y mínimo {2} caracteres", MinimumLength = 7)]
    [Required(ErrorMessage = "El campo {0} es requerido")]
    public string Email { set; get; }
    [Column("user_password")]
    [DataType(DataType.Password)]
    [Display(Name = "Contraseña")]
    [Required(ErrorMessage = "El campo {0} es requerido")]
    [StringLength(100, ErrorMessage = "El campo {0} puede contener máximo {1} y mínimo {2} caracteres", MinimumLength = 3)]
    public string Password { set; get; }
    [Column("city_id")]
    [Display(Name = "Ciudad")]
    [Required(ErrorMessage = "El campo {0} es requerido")]
    public int IdCity { set; get; }
    public virtual City City { set; get; }
    [Column("user_id_created")]
    [Display(Name = "Creado por usuario")]
    [Required(ErrorMessage = "El campo {0} es requerido")]
    public int IdUserCreated { set; get; }
}
    
asked by Straad 07.03.2016 в 18:00
source

1 answer

2

If you use migrations to create the structure of the database

Use Code First Migrations to Seed the Database

you could during this operation create the user in the method Seed()

Seeding ASP.NET Identity Database

In the class that creates migrations you could use the classes

 protected override void Seed(ProfileManagement.Models.ApplicationDbContext context)
{
    var store = new UserStore<ApplicationUser>(context);
    var manager = new UserManager<ApplicationUser>(store);
    var hasher = new PasswordHasher();
    var user = new ApplicationUser
    {
        UserName = "abhimanyu",
        PasswordHash = hasher.HashPassword("abhimanyu"),
        UserProfileInfo = new UserProfileInfo
        {
            FirstName = "Abhimanyu K",
            LastName = "Vatsa",
            EmailID = "[email protected]"
        }
    };

    manager.Create(user, "abhimanyu");
}
    
answered by 07.03.2016 / 18:14
source