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; }
}