User and Admin is a 1 to 1 relationship where there's only 1 admin role for 1 user.
Previously I fixed a ModelValidationException by adding [Key] on the first line of my administrator class following this link Click here but now I got this error:
Unable to determine the principal end of an association between the types 'RecreationalServicesTicketingSystem.Models.Administrator' and 'RecreationalServicesTicketingSystem.Models.User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
I tried following this solution but it still gave me the same error.Click Here
User.cs
public class User
{
public int UserID { 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; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime EnrollmentDate { get; set; }
public int DepotID { get; set; }
public int DepartmentID { get; set; }
public int TicketID { get; set; }
public string FullName
{
get { return LastName + ", " + FirstMidName; }
}
//Setting up relationships A use can apply for any number of tickets, so Tickets is defined as a collection of Ticket entities.
public virtual ICollection<Ticket> Tickets { get; set; }
//Setting up relationships Users can only have ONE adminstrator so we use public virtual Administrator Administrator { get; set; }
public virtual Administrator Administrator { get; set; }
public virtual Department Department { get; set; }
public virtual Depot Depot { get; set; }
}
Administrator.cs
public class Administrator
{
[Key]
public int AdminID { get; set; }
public int TicketID { get; set; }
public int UserID { get; set; }
[StringLength(50)]
public string AdminRole { get; set; }
public virtual ICollection<Ticket> Tickets { get; set; }
public virtual User User { get; set; }
}
This is in the configuration.cs where I do the migration for user and administrator.
User
var users = new List<User>
{
new User { FirstMidName = "lala", LastName = "la",
EnrollmentDate = DateTime.Parse("2016-02-18") },
new User { FirstMidName = "baba", LastName = "baba",
EnrollmentDate = DateTime.Parse("2016-02-18") },
new User { FirstMidName = "dada", LastName = "dada",
EnrollmentDate = DateTime.Parse("2016-02-18") },
new User { FirstMidName = "Christine", LastName = "West",
EnrollmentDate = DateTime.Parse("2016-02-18") },
};
Administrator
var administrator = new List<Administrator>
{
new Administrator {AdminID = 1, AdminRole = "Administrator LVL1", User = users.Single ( s => s.UserID == 1),
Tickets = new List<Ticket>() },
new Administrator {AdminID = 2, AdminRole = "Administrator LVL2", User = users.Single ( s => s.UserID == 2),
Tickets = new List<Ticket>() },
new Administrator {AdminID = 3, AdminRole = "Administrator LVL3", User = users.Single ( s => s.UserID == 3),
Tickets = new List<Ticket>() }
};
administrator.ForEach(s => context.Administrators.AddOrUpdate(p => p.AdminID, s));
context.SaveChanges();
}

AdminIdand make theUserIdthe primary key. Further, you should configure the principal end of the relation, i.e. the direction the cascading delete should take. It most likely that when the user is deleted the admin should be deleted too, so the User should be the principal. See entityframeworktutorial.net/code-first/…