0

I'm currently cloning a MVC5 with Identity. I was previously coding a MVC4 Code First Entity Framework application and wanted to upgrade it to MVC5 because of Identitiy. I'm currently trying to add a controller but I need to choose a data context class. In MVC4 I made a dbcontext called Issue Context and was wondering how do I convert it so it's compatitble with ApplicationDbContext(MVC5)

IssueContext.cs in MVC4

public class IssueContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Ticket> Tickets { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Depot> Depots { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

    }
}

ApplicationdbContext in IdentitiyModels.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    static ApplicationDbContext()
    {
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

Updated MV5 ApplicationDBContext.cs

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> 
        GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager
            .CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    public int UserID { get; set; } <---We don't need this right?

    public bool IsAdministrator { get; set; }
    [StringLength(50, MinimumLength = 1)]
    public string LastName { get; set; }
    [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

    [Column("FirstName")]
    public string FirstMidName { get; set; }

    public string FullName
    {
        get { return FirstMidName + " " + LastName; }
    }
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime EnrollmentDate { get; set; }

    public int DepartmentID { get; set; }
    [ForeignKey("DepartmentID")]
    public virtual Department Department { get; set; }
    public int DepotID { get; set; }
    [ForeignKey("DepotID")]
    public virtual Depot Depot { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }
}


public class ApplicationRole : IdentityRole
{
    public ApplicationRole() : base() { }
    public ApplicationRole(string name) : base(name) { }
    public string Description { get; set; }

}


public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }


    public DbSet<Ticket> Tickets { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<Depot> Depots { get; set; }

    static ApplicationDbContext()
    {
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

}

2 Answers 2

0

you need to make User class inherit from IdentityUser then remove users dataset while other data set remains intact.

public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext()
    : base("DefaultConnection")
{
}

public DbSet<Ticket> Tickets { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Depot> Depots { get; set; }

static ApplicationDbContext()
{
    Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}

public static ApplicationDbContext Create()
{
    return new ApplicationDbContext();
}
}


public class User : IdentityUser{

  // other properties
}
Sign up to request clarification or add additional context in comments.

1 Comment

I've redited the question and added the updated version. Is that how we do it?? We don't need UserID because IdentityUser has it right?
0

IdentityDbContext inherits DbContext. You can just add your DbSets from your original context to the ApplicationDbContext. However, you could use two separate Contexts, that way you don't have the identity tooling muddying up your database.

1 Comment

So when I add a controller I just redirect it to my IssueContext?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.