0

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

enter image description 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();
}
8
  • In a one to one relation the entities must share the same primary key. So remove the AdminId and make the UserId the 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/… Commented Feb 28, 2016 at 21:08
  • I tried following this solution I don't see how you used that proposed mapping. Commented Feb 28, 2016 at 23:03
  • @GertArnold I tried it but deleted it since it didn't solve my problem Commented Feb 29, 2016 at 0:00
  • @Dabblernl But I need adminID because adminID identifies whether the user is a Administrator LVL1 or Administrator LVL2 etc? I put [Key, ForeignKey("User")] on top of public int UserID { get; set; } Commented Feb 29, 2016 at 2:32
  • @GertArnold That didn't make any sense. Commented Feb 29, 2016 at 7:56

0

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.