2016-03-26 1 views
0

Je clone actuellement un MVC5 avec Identity. Auparavant, je codais une application MVC4 Code First Entity Framework et je voulais la mettre à niveau vers MVC5 à cause de Identitiy. J'essaye actuellement d'ajouter un contrôleur mais je dois choisir une classe de contexte de données. En MVC4 j'ai fait un dbcontext appelé contexte de l'émission et je me demandais comment puis-je le convertir il est donc compatitble avec ApplicationDbContext (MVC5)Conversion de MVC4 Dbcontext en MVC5 ApplicationDbContext

IssueContext.cs dans 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 dans 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(); 
    } 
} 

Mise à jour 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(); 
    } 
} 

}

Répondre

0

Vous devez faire en sorte que la classe User hérite de IdentityUser, puis supprimer le jeu de données utilisateur alors que les autres jeux de données restent intacts.

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 
} 
+0

J'ai répondu à la question et ajouté la version mise à jour. Est-ce ainsi que nous le faisons? Nous n'avons pas besoin d'userID car IdentityUser l'a bien? – TykiMikk

+0

C'est vrai, vous n'avez pas besoin de l'userId –

0

IdentityDbContext hérite de DbContext. Vous pouvez simplement ajouter vos DbSets de votre contexte d'origine à ApplicationDbContext. Cependant, vous pouvez utiliser deux Contextes séparés, de cette façon vous n'avez pas l'outil d'identité qui embrouille votre base de données.

+0

Donc quand j'ajoute un contrôleur, je le redirige juste vers mon IssueContext? – TykiMikk