Search code examples
c#asp.net-mvcentity-frameworkcode-first

Entity Framework Code First Create Database


I am new to Entity Framework. I want to make it Database as a separate Entity In project. So I started my project with as Class Library project. Starting with Login so class created for it with inherit IdentityUser.

public class ApplicationUser : IdentityUser
{
    [Required]
    [MaxLength(200)]
    public string FirstName { get; set; }
    [Required]
    [MaxLength(500)]
    public string LastName { get; set; }
}

Now I want to create database , I have added key in App.Config.

public class AppDBContext : DbContext
{
    public AppDBContext():base("AppDBConnectionString")
    {

    }
}

Any Package Manager command to create database for first time ? Update-Database didnt work.


Solution

  • Ok, to summarize, update your class to include a key:

    public class ApplicationUser : IdentityUser
    {
    [Key]
    public int ApplicationUserId { get; set; }
    
    [Required]
    [MaxLength(200)]
    public string FirstName { get; set; }
    [Required]
    [MaxLength(500)]
    public string LastName { get; set; }
    }
    

    And update your DbSet in your AppDbContext to:

    DbSet<ApplicationUser> ApplicationUsers { get; set; }