Reputation: 1068
Previously I was coding a MVC4 application and these are my classes: Ticket,Category,Department,Depot and User(Now is an Identity User in IdentityModels.cs)
The relationship between the classes are as follows:
One to Many User to Ticket (One user can open lots of tickets)
One to Many Category to Ticket (One Ticket can have one Category)
One to One Department to User (One user can only have one Department)
One to One Depot to User (One user can only have one Depot)
My question is because I've upgraded from MVC4 to MVC5, MVC5 user must derive from IdentityUser so I must delete my User.cs and put the code into my ApplicationUser : IdentityUser
class(shown below)
After doing that then my classes with public virtual ICollection<User> Users { get; set; }
or public virtual User User { get; set; }
will get errors since they aren't referencing to anything(because I've deleted my User class).
The type or namespace name 'User' could not be found (are you missing a using directive or an assembly reference?)
What do I need to change that code into so it references my ApplicationUser : IdentityUser
class?
IdentityModels.cs (Includes the new user class that derives from Identity User)
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager
.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
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();
}
}
Depot.cs
public class Depot
{
public int DepotID { get; set; }
[StringLength(50, MinimumLength = 1)]
public string DepotName { get; set; }
public virtual ICollection<User> Users { get; set; } <--Error
}
Department.cs
public class Department
{
public int DepartmentID { get; set; }
[StringLength(50, MinimumLength = 1)]
public string DepartmentName { get; set; }
public virtual ICollection<User> Users { get; set; } <--Error
}
Ticket.cs
public class Ticket
{
public int? TicketID { get; set; }
[Required(ErrorMessage = "Please enter the description")]
public string Issue { get; set; }
[Display(Name = "Administrator")]
[Required(ErrorMessage = "Please select the Administrator")]
public int IssuedTo { get; set; }
public int Author { get; set; }
[DisplayFormat(NullDisplayText = "No Priority")]
public Priority Priority { get; set; }
[ForeignKey("CategoryID")]
public virtual Category Category { get; set; }
public int CategoryID { get; set; }
public int UserID { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; } <--Error
}
OLD user.cs file NOT included in solution
public class User
{
public int UserID { get; set; }
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; }
}
Start of Configuration.cs(Seed method)
internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
{
Upvotes: 1
Views: 84
Reputation: 247571
You are basically recreating the relationship/navigation between the entities.
Depot.cs
public class Depot {
//....code removed for brevity
public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}
Department.cs
public class Department {
//....code removed for brevity
public virtual ICollection<ApplicationUser> Users { get; set; } // fixed
}
Ticket.cs
public class Ticket {
//....code removed for brevity
public string UserID { get; set; } // fixed
[ForeignKey("UserID")]
public virtual ApplicationUser User { get; set; } // fixed
}
Upvotes: 1